Hi!
I can write a program to find a string in a file, write it to a different file, increment an int number in the file. But I can't find an example to find a line in a file which contains the string I need and then print the whole line.
Example:
search for String: 0x22 0x023 0x24
if String is found on line 9
print the whole line 9, or print until you find a custom terminating character like "0x9999 "
result must be: print 0x22 0x23 0x24 0x25 0x26 0x27 0x28.
Code:
#include <stdio.h>
int main(void)
{
FILE *ifp, *ofp;
char *mode = "r";
char outputFilename[] = "out.list";
char username[9]; /* One extra for nul char. */
int score;
ifp = fopen("in.list", mode);
if (ifp == NULL) {
fprintf(stderr, "Can't open input file in.list!\n");
exit(1);
}
ofp = fopen(outputFilename, "w");
if (ofp == NULL) {
fprintf(stderr, "Can't open output file %s!\n", outputFilename);
exit(1);
}
while (fscanf(ifp, "%s %d", username, &score) == 2) {
fprintf(ofp, "%s %d\n", username, score+10);
}
fclose(ifp);
fclose(ofp);
return 0;
}
EDIT:
The simplest best I found is to find the string I am looking for(0x22 0x23 0x24 in this case) and print until the custom terminating character, but I don't how does the auto incrementation of fscanf will work in this case and I want to know the other case too.