I got stuck in some basic file handling code in C. Basically I want to parse an input file to get some information, and then put it in another file (more information is in the input file). I used an online gcc compiler for that and all was great. When Ienter code here
compile the code in cmd with gcc command output file is empty.
#include <stdio.h>
#include <string.h>
int main() {
char c[1000];
FILE *fptr,*resultfp;
if ((fptr = fopen("inputfile.txt", "r")) == NULL) {
printf("can't be opened");
}
if ((resultfp = fopen("outputfile.txt", "w")) == NULL) {
printf("can't be opened");
}
while(strcmp(c,"END OF FILE")){
fscanf(fptr, "%[^\n]", c);
fseek(fptr, 1,SEEK_CUR);
if(strstr(c,"Example name") || strstr(c,"Example description") )
{
fscanf(fptr, "%[^\n]", c);
fprintf(resultfp,"%s\n", c);
}
}
fclose(fptr);
fclose(resultfp);
return 0;
}
and this is input file:
This is an example
Example name:
example1
Example description:
description1
Example name:
example2
Example description:
description2
I want to parse this file
In order to get example name and example description
END OF FILE