Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fseek() - generic question dued to a problem

fseek() - generic question dued to a problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lockhart
    wrote on last edited by
    #1

    I have a save-file from my program so structured:

    char id[32]; // "DATASET_1"
    int blocksize;
    :
    : <- various data for an amount of 'blocksize' bytes
    :

    char id[32]; // "DATASET_2"
    int blocksize;
    :
    : <- various data for an amount of 'blocksize' bytes
    :

    char id[32]; // "DATASET_3"
    int blocksize;
    :
    : <- various data for an amount of 'blocksize' bytes
    :

    With a generic number of "DATASET_#" When I try to load the file, I use code similiar to this:

    char id[32];
    int blocksize;
    FILE* f;

    f = fopen(filename, "rb");

    while (!feof(f))
    {
    fread(id,sizeof(char),32,f); // read id
    fread(&blocksize,sizeof(int),1,f); // read blocksize

    if (strcmp(id,"DATASET\_1") == 0)
    {
    	fread( /\*  appropriate reading here \*/);
    }
    else if (strcmp(id,"DATASET\_2") == 0)
    {
    	fread( /\*  appropriate reading here \*/);
    }
    else // DATASET unknown
    {
    	// skip unknow block of data
    	fseek(f,blocksize,SEEK\_CUR);
    }
    

    }//while

    fclose(f);

    When I do this, the while loop never exits if, for example, the last dataset is unknown, it seems that the fseek() function has no effect on the next "while (!feof(f))" call. I must take care of something about fseek under NT or WinXP or I must look for an error elsewhere ? By now, I assume the data to be written well. Thanks in advance for any suggestion.

    C 1 Reply Last reply
    0
    • L Lockhart

      I have a save-file from my program so structured:

      char id[32]; // "DATASET_1"
      int blocksize;
      :
      : <- various data for an amount of 'blocksize' bytes
      :

      char id[32]; // "DATASET_2"
      int blocksize;
      :
      : <- various data for an amount of 'blocksize' bytes
      :

      char id[32]; // "DATASET_3"
      int blocksize;
      :
      : <- various data for an amount of 'blocksize' bytes
      :

      With a generic number of "DATASET_#" When I try to load the file, I use code similiar to this:

      char id[32];
      int blocksize;
      FILE* f;

      f = fopen(filename, "rb");

      while (!feof(f))
      {
      fread(id,sizeof(char),32,f); // read id
      fread(&blocksize,sizeof(int),1,f); // read blocksize

      if (strcmp(id,"DATASET\_1") == 0)
      {
      	fread( /\*  appropriate reading here \*/);
      }
      else if (strcmp(id,"DATASET\_2") == 0)
      {
      	fread( /\*  appropriate reading here \*/);
      }
      else // DATASET unknown
      {
      	// skip unknow block of data
      	fseek(f,blocksize,SEEK\_CUR);
      }
      

      }//while

      fclose(f);

      When I do this, the while loop never exits if, for example, the last dataset is unknown, it seems that the fseek() function has no effect on the next "while (!feof(f))" call. I must take care of something about fseek under NT or WinXP or I must look for an error elsewhere ? By now, I assume the data to be written well. Thanks in advance for any suggestion.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      from the MSDN for fseek: " You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. " fseek doesn't care about EOF. -c


      "What the hell are you getting so upset about? I thought you didn't believe in God".    "I don't," she sobbed, bursting violently into tears, "but the God I don't believe in is a good God, a just God, a merciful God. He's not the mean and stupid God you make Him out to be".    -- Joseph Heller Smaller Animals Software, Inc.

      L 1 Reply Last reply
      0
      • C Chris Losinger

        from the MSDN for fseek: " You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream. " fseek doesn't care about EOF. -c


        "What the hell are you getting so upset about? I thought you didn't believe in God".    "I don't," she sobbed, bursting violently into tears, "but the God I don't believe in is a good God, a just God, a merciful God. He's not the mean and stupid God you make Him out to be".    -- Joseph Heller Smaller Animals Software, Inc.

        L Offline
        L Offline
        Lockhart
        wrote on last edited by
        #3

        Ok, but does feof() take care of a previous fseek() ? This loop never exits in a test program I've done... try it out plz if you can:

        FILE* f;
        long i;

        f = fopen("c:\\a.txt","rb");

        i = 0;

        while (!feof(f))
        {
        fseek(f,i++,SEEK_SET); // seeks at 0,1,2,3,... until reach EOF (?)
        }

        fclose(f);

        The file "a.txt" contains three simple characters.

        C 1 Reply Last reply
        0
        • L Lockhart

          Ok, but does feof() take care of a previous fseek() ? This loop never exits in a test program I've done... try it out plz if you can:

          FILE* f;
          long i;

          f = fopen("c:\\a.txt","rb");

          i = 0;

          while (!feof(f))
          {
          fseek(f,i++,SEEK_SET); // seeks at 0,1,2,3,... until reach EOF (?)
          }

          fclose(f);

          The file "a.txt" contains three simple characters.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Is your program C ( not C++) ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

          L 1 Reply Last reply
          0
          • C Christian Graus

            Is your program C ( not C++) ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

            L Offline
            L Offline
            Lockhart
            wrote on last edited by
            #5

            It's C++, MFC app. By now I have solved with something like this:

            while (fread(id,sizeof(char),32,f) != 0)
            {
            // almost same stuff here
            }

            But I thought that it had should exit if fseek() seeks the position over the EOF marker...

            C 1 Reply Last reply
            0
            • L Lockhart

              It's C++, MFC app. By now I have solved with something like this:

              while (fread(id,sizeof(char),32,f) != 0)
              {
              // almost same stuff here
              }

              But I thought that it had should exit if fseek() seeks the position over the EOF marker...

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              OK - I am curious, is there a reason you're using C functions ? What advantage do you get from fread, as opposed to C++ equivelants, souch as ifstream ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

              L 1 Reply Last reply
              0
              • C Christian Graus

                OK - I am curious, is there a reason you're using C functions ? What advantage do you get from fread, as opposed to C++ equivelants, souch as ifstream ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

                L Offline
                L Offline
                Lockhart
                wrote on last edited by
                #7

                Only because I'm in the abit of using them, so I can reuse some old code :) Do you suggest me using ifstream instead ?

                C 1 Reply Last reply
                0
                • L Lockhart

                  Only because I'm in the abit of using them, so I can reuse some old code :) Do you suggest me using ifstream instead ?

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  I would always recommend using C++ code over C equivelants, the C++ libraries were written not just to do the same as the C stuff, but to do it better. I was seriously asking, because I notice a lot of people posting C code and I'm happy to learn that it does some things better, better to ask and find out than wade in with my opinion and be told ;-) Your code looked to me like it would be a lot more readable using ifstream though, although I admit I only glanced over it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

                  L 1 Reply Last reply
                  0
                  • C Christian Graus

                    I would always recommend using C++ code over C equivelants, the C++ libraries were written not just to do the same as the C stuff, but to do it better. I was seriously asking, because I notice a lot of people posting C code and I'm happy to learn that it does some things better, better to ask and find out than wade in with my opinion and be told ;-) Your code looked to me like it would be a lot more readable using ifstream though, although I admit I only glanced over it. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

                    L Offline
                    L Offline
                    Lockhart
                    wrote on last edited by
                    #9

                    I'm rewriting the functions using fstream, if you say fstream acts better, I trust you :) Thanks for the advice :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups