Page 1 of 1

Need help with file reading & writing

PostPosted: Fri May 06, 2011 7:25 pm
by lcl
So, I need help with this problem.
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! :)

Re: Need help with file reading & writing

PostPosted: Fri May 06, 2011 8:16 pm
by skydereign
Try this. One thing you were doing is having it reprint every line, which isn't necessary if you just want to add the new string (resetString might be the reason you are doing that though). The problem is that using fgets will append the new line to the end of the string. Several fixes for that, but I just used fscanf as that does not.
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.
    fscanf(wallpapers, "%s", walls[k]);
  }

  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);
    fputs(walls[last], wallpapers);
    cannot = 1;
  }
  fclose(wallpapers);
}

Re: Need help with file reading & writing

PostPosted: Fri May 06, 2011 8:27 pm
by lcl
Thanks, I have to try that one. :)
I didn't know how fscanf() works, that's why I didn't use it.

Re: Need help with file reading & writing

PostPosted: Fri May 06, 2011 10:27 pm
by lcl
Thanks, it works perfectly! :D
But can you explain me how fscanf() works?

Re: Need help with file reading & writing

PostPosted: Fri May 06, 2011 11:17 pm
by skydereign
fscanf(FILE * stream, const char* format, ...);

So, you know FILE*, and the const char * format is just like you would do with sprintf. So "%s" means you are looking for a string. For all the variables in the format, it looks for it and places it into the addresses specified in the ... Kind of a quick explanation, but it works very similar to sprintf, just reversed and using files.

Re: Need help with file reading & writing

PostPosted: Fri May 06, 2011 11:28 pm
by lcl
Oh, thank you very much!
I understand it now! :)