How to find if string exists in file?
Posted: Wed Dec 12, 2012 4:37 am
I am trying to find a way to check if a string already exists in a text file. If the string does not exist, then I want to append it to the file. This code I made does not work, can anyone help?
- Code: Select all
char MapName[256] = "Name";
char line[256];
int found = 0;
FILE * levs = fopen("levels.text", "r");
while(fgets(line, 255, levs)) {
if(strcmp(line, MapName) == 0) {
found = 1;
}
}
fclose(levs);
if(!found) {
FILE * GO = fopen("levels.txt", "a+");
fprintf(GO, "%s\n", MapName);
fclose(GO);
}