I want to write inputted line to file, but just if it isn't
there already. I also want to store the lines to array of strings.
The function works great otherwise, but it writes the line even if it already exists.
My code in global code is this:
- Code: Select all
char walls[50][255];
void validWallpapers(char new[255])
{
int i = 0, j = 0, k = 0, cannot = 0, last = 0, stopped = 0;
FILE * wallpapers = fopen("wallpaper_config.dat", "r+");
for (k = 0; k < 49; k ++)
{
resetString(walls[k]); //resetString is my own function.
fgets(walls[k], 255, wallpapers);
}
for (i = 0; i < 49; i ++)
{
if (strcmp(walls[i], new) == 0){cannot = 1;}
if (strcmp(walls[i], "") == 0){last = i; stopped = 1; break;}
}
if (cannot == 0 && stopped == 1)
{
sprintf(walls[last], "%s\n", new);
for (j = 0; j < 49; j ++)
{
fputs(walls[j], wallpapers);
}
cannot = 1;
}
fclose(wallpapers);
}
And my function call in view create actor is this:
- Code: Select all
validWallpapers("desktop0.bm");
It might be something simple or then maybe not but I can't figure it out even I've tried it for hours.
Please help me!