why doesnt ifstream::seekg(...) works on text files?

This is a discussion on why doesnt ifstream::seekg(...) works on text files? within the C++ Programming forums, part of the General Programming Boards category; hello, okey, i opened the file, count the number of lines inside it by doing this, ifstream fin("text"); while(text.getline(buffer, 1024) ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    why doesnt ifstream::seekg(...) works on text files?

    hello,

    okey, i opened the file, count the number of lines inside it by doing this,

    ifstream fin("text");
    while(text.getline(buffer, 1024)
    numberOfLines++;

    however, later on, i'd like to read it again from the beginning and i did this,

    fin.seekg(0, ios::beg)
    but when i did this again,
    text.geline(buffer, 1024)
    it fails??

    many thanks!!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Hey bud. Did you try my first method I told you about? That will retrieve the size of the file without messing with your file pointer. Then it will be at the beginning when you need it!
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hey mrwizard!

    yups! exactly,

    [source]
    // count the number of bytes inside the file
    finLump.seekg(0, ios::end); // put the pointer at the end of the file
    int size = finLump.tellg(); // get the pointer's position
    // restore the pointer to the beginning of the file
    finLump.seekg(0, ios::beg);
    unsigned char *data = new unsigned char[size];
    finLump.read(data, size);
    [/source]

    thanks!!

    however, on text file, those above methods won't work, i must put the pointer back on the beginning, why won't it work? (so what i did is, close and reopen the file), but i'd like to know why it won't work,

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The seekg function sets the position of the get pointer (next location to be read). If the file reaches EOF, the stream is placed in an error state. To reset this state you need to call the clear() function. You can check this by reading just 1 line and the set the position back to the beginning of the file (this will work).

    Insert the clear function just before repositioning the pointer:
    Code:
    fin.clear();
    fin.seekg(0, ios::beg)
    text.geline(buffer, 1024);

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    oh i see, thanks!!

    anyway more problems plz help,

    can i make ofstream to create directories for me? if so how?

    i've tried these which none works,

    ofstream fout("directory/file.dat");
    ofstream fout("/directory/file.dat");
    ofstream fout("/directory/");
    ofstream fout("/directory");

    many thanks!

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    There is no standard way to create a directory.
    It depends on your OS. You can do a board search to find out which functions to use (mkdir, CreateDirectory, etc.).

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    oh okey,

    anyway i got more problems? hehe, hope you guys don't mind,

    am just trying/testing to transfer the first 32 bytes of a .png graphic file to another file, but when i open in a hex editor the output file, it doesn't mater the png? so if i transfer the whole file of a png to another file, my graphic viewer won't be able to open the .png...

    ie,
    //original png:
    0x89 50 4e 47 0d 0a 1a 0a 00 00 00...
    // output
    0x89 50 4e 47 0d 0d 0a 1a 0d 0a 00...

    see? what's with the 0d?? according to the ascii chracter set, it's either a tab, linefeed, carriage return etc., but why is it inserted there?

    this is how i copied the files,

    unsigned char data[32];
    ifstream fin("graphic.png");
    fin.read(data, 32);

    ofstream fout("output.png");
    fout.write((unsigned char*)&data, 32);

    many thanks!!!

    EDIT: whoops it was open as ios::in | ios::binary, okey i found out something, when i read in 5 bytes only, 5 bytes are actually written out too, but when i made it 6 bytes, it writes 7 bytes??
    ie,
    0x89 50 4e 47 0d 0d 0a should be,
    0x89 50 4e 47 0d 0a
    also, when i ommited the ios::in and ios::binary, it reads correctly only upto the 6th bytes? after that it's all garbage??

    thanks!!
    Last edited by mickey; 10-10-2002 at 12:38 PM.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    ifstream fin("graphic.png", ios::in | ios::binary);
    ofstream fout ("graphic2.png", ios:ut | ios::binary);

    *edit* whoops, it appears you have figured out the flags...oh well.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    no actually, you helped!!! thanks a lot! i had the flags only when reading the file, but i don't have it on ofstream, now i placed it and it works now well! thanks al ot guys for all your time!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 12:21 PM
  2. text files in c++
    By DungeonMaster in forum C++ Programming
    Replies: 5
    Last Post: 03-14-2006, 03:48 PM
  3. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM

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