simple problem about fseek()

This is a discussion on simple problem about fseek() within the C++ Programming forums, part of the General Programming Boards category; i have seen some thread discuss about using fseek() to tell the size of the file. from the msdn, it ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    simple problem about fseek()

    i have seen some thread discuss about using fseek() to tell the size of the file.

    from the msdn, it said it will return -1L if there is error.

    i would like to ask how to check if the return value is an error or not?(i think that is not an integer right?)

    thank you.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: simple problem about fseek()

    Originally posted by Jasonymk
    i have seen some thread discuss about using fseek() to tell the size of the file.

    from the msdn, it said it will return -1L if there is error.

    i would like to ask how to check if the return value is an error or not?(i think that is not an integer right?)

    thank you.
    If it succeeds, it returns zero. Otherwise, you can check errno for specific information about the error.
    Code:
    if (fseek(...) != 0)
    {
         switch (errno)
         {
             // ...
         }
    }
    See here: http://www.mkssoftware.com/docs/man3/fseek.3.asp

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117
    or sorry, i should ask about tellg()

    in fact my code is:
    Code:
    file6.seekg (0, ios::end);
        int file_size = file6.tellg();
        cout << "The file is " << file_size << " bytes." << endl;
    for a file it cant open, the out put will be :

    the file is -1 bytes

    i have tried to detect the error by if (filesize = -1) but fail, i wonder if the error no is an int or anythng else.

    thank you

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    67

    file size

    Code:
    // read a file into memory
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      int length;
      char * buffer;
    
      ifstream is;
      is.open ("test.txt", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
      length = is.tellg();
      is.seekg (0, ios::beg);
    
      // allocate memory:
      buffer = new char [length];
    
      // read data as a block:
      is.read (buffer,length);
    
      is.close();
    
      cout.write (buffer,length);
    
      return 0;
    }
    [1] http://www.cplusplus.com/ref/iostrea...eam/tellg.html

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Do you check if the stream is good ?

    Code:
    if ( file6.good() )   
    {
       file6.seekg (0, ios::end);
       int file_size = file6.tellg();
       cout << "The file is " << file_size << " bytes." << endl;
       file6.seekg (0, ios::beg);
    }
    else
    {
       cout << "There is a problem with the stream" << endl;
       //do error checking...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 09:49 AM
  5. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21