Here is a C Project. I am working with text and binary files. This is a project that reads from stdin a file and also reads a new file and writes the information that the existed file has to the new file. But i get an error .This is my code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fFrom;
FILE *fTo;
char filename[80];
char newfilename[80];
printf("Enter the file you want to copy: ");
scanf("%s", filename);
fFrom = fopen(filename, "r");
if(fFrom == NULL)
{
printf("File not found...");
return;
}
printf("Enter new filename: ");
scanf("%s", newfilename);
fTo = fopen(newfilename, "r");
if(fTo)
{
printf("File already exists...\n");
return;
}
fclose(fTo);
fTo = fopen(newfilename, "w");
if(!fTo)
{
printf("Error opening file...\n");
return;
}
while (!feof(fFrom))
{
fputc(fgetc(fFrom), fTo);
}
fclose(fFrom);
fclose(fTo);
return;
}
Then In the console: Enter the file you want to copy: test.txt // existing file Enter new filename: new_test.txt // new file Segmentation fault (core dumped)