Need help with file reading & writing

Non-platform specific questions.

Need help with file reading & writing

Postby lcl » Fri May 06, 2011 7:25 pm

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! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Need help with file reading & writing

Postby skydereign » Fri May 06, 2011 8:16 pm

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);
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Need help with file reading & writing

Postby lcl » Fri May 06, 2011 8:27 pm

Thanks, I have to try that one. :)
I didn't know how fscanf() works, that's why I didn't use it.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Need help with file reading & writing

Postby lcl » Fri May 06, 2011 10:27 pm

Thanks, it works perfectly! :D
But can you explain me how fscanf() works?
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Need help with file reading & writing

Postby skydereign » Fri May 06, 2011 11:17 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Need help with file reading & writing

Postby lcl » Fri May 06, 2011 11:28 pm

Oh, thank you very much!
I understand it now! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest