I'm doing and small exercise where I choose a file and I want the content of the file in reverse, I got problems in the last loop, this is the error I got Process returned -1073741819 (0xC0000005) execution time : 5.427 s
Example:
File1:
Line1
Line2
Line3
File2:
Line3
Line2
Line1
This is my code
#define MAXCHAR 1000
int main()
{
FILE *fptr1, *fptr2;
char filename[MAXCHAR];
int i=0;
char *lines[4];
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL){
printf("Cannot open file %s \n", filename);
exit(0);
}
for(i=0;fgets(filename, MAXCHAR, fptr1) != NULL; i++){
lines[i] = filename;
//printf("%s", lines[i]);
}
//FILE 2
printf("\n Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
for(i = 4; i > 0;i--){
fputs(lines[i],fptr2);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
User contributions licensed under CC BY-SA 3.0