Same Question on FILE!

  • darren
  • Born
  • Born
  • darren
  • Posts: 3

Post 3+ Months Ago

Code: [ Select ]
FILE *fp;
  int i;
  
  fp = fopen("C:\\My Documents\\book\\seats.db", "w");
  if(fp == NULL)
  {
    printf("Can't create file: Data\\seats.db");
    exit(0);
  }
  else
  {
    for(i = 0; i < 8; i++)
    {
      fprintf(fp, "XXXXXXXX");
      if(i != 7)
      {
        fprintf(fp, "\n");
      }
    }    
    fclose(fp);
   
  }
}
  1. FILE *fp;
  2.   int i;
  3.   
  4.   fp = fopen("C:\\My Documents\\book\\seats.db", "w");
  5.   if(fp == NULL)
  6.   {
  7.     printf("Can't create file: Data\\seats.db");
  8.     exit(0);
  9.   }
  10.   else
  11.   {
  12.     for(i = 0; i < 8; i++)
  13.     {
  14.       fprintf(fp, "XXXXXXXX");
  15.       if(i != 7)
  16.       {
  17.         fprintf(fp, "\n");
  18.       }
  19.     }    
  20.     fclose(fp);
  21.    
  22.   }
  23. }


i have created a file with these 'X' char. I would like to seek for a point...
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
e.g above is wat in the file that i saved and i would like to select a point thati want to overwrite with a letter 'O'.Like this example below

XXXXXXX
XXXXXXX
XXOXXXX
XXXXOXX

Can anyone help me please? Hope you can understand wat i trying to say.
Thanx!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

:shock: what the....

hehe I have no idea what you are asking, but perhaps someone else
will. sorry...
  • billstack
  • Newbie
  • Newbie
  • billstack
  • Posts: 5

Post 3+ Months Ago

If you want to use files, then do something like the following. A more effecient approach would keep everything in memory, but this may not be what you want. Also note that '\n' can become different character combinations on different systems, so I modified your original code to use (char)10 as the row seperator.

One final warning: the following code does only what you asked, it is not effecient at all, and should never be used in any time critical code, or in any code where changeSeat is called regularly (or where the database is large).

Code: [ Select ]
#include <stdio.h>

#define SEATS_DATABASE "seats.db"
#define NUM_ROWS    8
#define NUM_SEATS   8

void initDataBase(void)
{
  FILE *fp;
  int i;
  int j;

  fp = fopen(SEATS_DATABASE, "w");
  if(fp == NULL)
  {
    printf("Can't create file");
    exit(0);
  }
  else
  {
    for(i=0; i<NUM_ROWS; ++i)
    {
      for(j=0; j<NUM_SEATS; ++j)
        fputc('X', fp);
      if(i != NUM_ROWS-1)
        fputc(10, fp);
    }
    fclose(fp);
  }
}

void changeSeat(int row, int seat, char ch)
{
  FILE* fp;

  fp = fopen(SEATS_DATABASE, "r+");
  if (fp)
  {
    fseek(fp, row*(NUM_SEATS+1)+seat, SEEK_SET);
    fputc(ch, fp);
    fclose(fp);
  }
  else
  {
    printf("Can't update the database\n");
  }
}

int main(void)
{
  initDataBase();

  changeSeat(3, 3, 'O');
  changeSeat(0, 1, 'O');

  return 0;
}
  1. #include <stdio.h>
  2. #define SEATS_DATABASE "seats.db"
  3. #define NUM_ROWS    8
  4. #define NUM_SEATS   8
  5. void initDataBase(void)
  6. {
  7.   FILE *fp;
  8.   int i;
  9.   int j;
  10.   fp = fopen(SEATS_DATABASE, "w");
  11.   if(fp == NULL)
  12.   {
  13.     printf("Can't create file");
  14.     exit(0);
  15.   }
  16.   else
  17.   {
  18.     for(i=0; i<NUM_ROWS; ++i)
  19.     {
  20.       for(j=0; j<NUM_SEATS; ++j)
  21.         fputc('X', fp);
  22.       if(i != NUM_ROWS-1)
  23.         fputc(10, fp);
  24.     }
  25.     fclose(fp);
  26.   }
  27. }
  28. void changeSeat(int row, int seat, char ch)
  29. {
  30.   FILE* fp;
  31.   fp = fopen(SEATS_DATABASE, "r+");
  32.   if (fp)
  33.   {
  34.     fseek(fp, row*(NUM_SEATS+1)+seat, SEEK_SET);
  35.     fputc(ch, fp);
  36.     fclose(fp);
  37.   }
  38.   else
  39.   {
  40.     printf("Can't update the database\n");
  41.   }
  42. }
  43. int main(void)
  44. {
  45.   initDataBase();
  46.   changeSeat(3, 3, 'O');
  47.   changeSeat(0, 1, 'O');
  48.   return 0;
  49. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 90 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.