<fstream> again !!!!!

This is a discussion on <fstream> again !!!!! within the C++ Programming forums, part of the General Programming Boards category; I'm using fstream to load a *.txt file with about 1.3 MB of text. I have a Code: char text[10000]; ...

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    Angry <fstream> again !!!!!

    I'm using fstream to load a *.txt file with about 1.3 MB of text. I have a
    Code:
    char		text[10000];
    ifstream	fin;
    Since whole file can't be put into "text[10000]", i'm doing it multiple times
    Code:
    while(true)
    {
    	fin.read(text, 9999);
    	text[9999] = NULL;
    	... writing this text somewhere
    	if(fin.eof())break;
    }
    The problem is that "fin.read()" refuses to read the text last time, I have loaded
    varius files, and it always skiped reading last time. And eof() returns true no matter
    "fin.read" refused...

    Is there a solution to this???
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Are you zeroing out the buffer after each read, because here is what I suspect is happening.
    You read in 9999, write it, read in another 9999 overwriting old data. Now when it gets to the leftovers at the end of the file that is less that 9999, (for instances, let's say 100), it's reading those 100 and overwriting the first 100 of the last 9999. Thus when you look at the end of what you wrote you are thinking, where is the end of my data? (it's at the beginning of this 9999 block)

    I believe read returns number of bytes read, you could use that number to place your terminating null instead of just blindly at 9999, that way the last read will work... or you can 0 out in between, but that will be a little slower.
    Last edited by Darryl; 12-19-2005 at 02:49 PM.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    wow, no offense meant, but that's a terrible loop... something better would be:
    Code:
    while(fin.read(text,9999))
    {
    	text[9999] = NULL;
    	// ... writing this text somewhere
    }
    but that looks really strange anyway, and I'd probably use
    Code:
    char ch;
    while(file.get(ch))
    {
    	// ... writing this text somewhere
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This works fine for me
    Code:
    #define BUFSZ 100
    int main() {   
       char buffer[BUFSZ];
       ifstream in("xx.c", ios::binary);
       int total=0;
       while ( in.read(buffer,BUFSZ-1) ) {
          int count = in.gcount(); // get nr bytes read
          total += count;
          buffer[count]=0;       // append terminating 0
          cout << buffer;
       }
       cout << endl << total << " bytes read" << endl;
    }

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    null != 0

    you probably wanted to say: buffer[count]='\0';

    ...and please avoid usimg macros... instead of using #define BUFSZ 100, use const short int BUFSZ=100; in main.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Darryl
    Are you zeroing out the buffer after each read, because here is what I suspect is happening.
    You read in 9999, write it, read in another 9999 overwriting old data. Now when it gets to the leftovers at the end of the file that is less that 9999, (for instances, let's say 100), it's reading those 100 and overwriting the first 100 of the last 9999. Thus when you look at the end of what you wrote you are thinking, where is the end of my data? (it's at the beginning of this 9999 block)

    I believe read returns number of bytes read, you could use that number to place your terminating null instead of just blindly at 9999, that way the last read will work... or you can 0 out in between, but that will be a little slower.
    That's not happening. I'm not an idiot...
    Heres what i found on MSDN:
    Code:
    The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. Extraction stops early on end of file, in which case the function calls setstate(failbit). In any case, it returns *this.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You should consider Derryl's posting. Just tested it. He's right . My solution works but don't use a macro
    Kurt

  8. #8
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by ElastoManiac
    That's not happening. I'm not an idiot...
    Heres what i found on MSDN:
    Code:
    The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. Extraction stops early on end of file, in which case the function calls setstate(failbit). In any case, it returns *this.
    Ok, I am an IDIOT. I fixed the problem.
    I can't belive how stupid i am.............
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by major_small
    null != 0

    you probably wanted to say: buffer[count]='\0';
    actually null does = 0

    try
    Code:
    int null = '\0';
    cout << null;
    also see Stroustrups answer to Should I use NULL or 0?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use <fstream> and ostream overloading together
    By mosu' in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2006, 02:06 PM
  2. <fstream> arghghghhhhhhhh
    By ElastoManiac in forum C++ Programming
    Replies: 26
    Last Post: 12-02-2005, 06:38 AM
  3. Debug Help w/ <fstream>
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2005, 05:14 PM
  4. About "fstream" in <fstream.h> and <fstream>
    By L.O.K. in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2005, 06:49 PM
  5. allegro weirdness, <fstream> errors
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 02-16-2003, 09:46 PM

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