Error reading in file in binary mode

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Error reading in file in binary mode

 
0
  #1
Jul 14th, 2005
Guys,

Either there is a bug in the compiler or I'm getting dumber by the day. I want to read an entire file into memory. (Very small file, so no worries about size here). I followed the example here (from www.cplusplus.com)
  1. // reading binary file
  2. #include <iostream.h>
  3. #include <fstream.h>
  4.  
  5. const char * filename = "example.txt";
  6. int main ()
  7. {
  8. char * buffer;
  9. long size;
  10. ifstream file (filename, ios::in|ios::binary|ios::ate);
  11. size = file.tellg();
  12. file.seekg (0, ios::beg);
  13. buffer = new char [size];
  14. file.read (buffer, size);
  15. file.close();
  16. cout << "the complete file is in a buffer";
  17. delete[] buffer;
  18. return 0;
  19. }
<< moderator edit: added [code][/code] tags and reformatted >>

However, when I use this code, I always get the first character of the file twice!!! For example, if I have a file which contains:
SAM LOVES TO TEST FILES

I end up with:
SSAM LOVES TO TEST FILES

Is this a known bug or am I not supposed to believe this reputable site? Is there a 'C' way to do this which is better?

Thanks,
Winbatch
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #2
Jul 14th, 2005
I didn't see the issue, but WRT a C version I think it would be something like this.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. static const char filename[] = "file.txt";
  7. FILE *file = fopen(filename, "rb");
  8. if ( file )
  9. {
  10. char *buffer;
  11. long size;
  12. fseek(file, 0, SEEK_END);
  13. size = ftell(file);
  14. rewind(file);
  15. buffer = malloc(size);
  16. if ( buffer )
  17. {
  18. if ( fread(buffer, size, 1, file) == 1 )
  19. {
  20. /* puts(buffer); */
  21. }
  22. }
  23. fclose(file);
  24. free(buffer);
  25. }
  26. return 0;
  27. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #3
Jul 14th, 2005
Dave,

When you used the code I posted you didn't have any trouble? Wonder if it's a compiler specific bug - I'm using Sun Workshop 6 on solaris... I tried your code and it works... I put the puts(buffer) back ;)


Winbatch
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #4
Jul 14th, 2005
First, I rarely start by looking at the compiler; I'd say about 99% of the time it is the code. Just because I didn't see what you saw doesn't mean there wasn't an issue with it.

I think in part what is missing is what is missing. How do you see what you see? Where is some sort of output statement?

One of the reasons I put the puts line in comments is that it probably isn't what you are doing and it's probably not the correct thing to do. And perhaps that is the issue -- on the output side, not the input side.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #5
Jul 14th, 2005
This was the code:
// reading binary file
#include <iostream.h>
#include <fstream.h>
const char * filename = "test.txt";
int main ()
{
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
size = file.tellg();
file.seekg (0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
file.close();
cout<<buffer<<endl;
delete[] buffer;
return 0;
}


(Also note that cout<<buffer[0]<<endl; and cout<<buffer[1]<<endl; also produced the same letter.....)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #6
Jul 14th, 2005
Hmm. When I run either C or C++ version in the command shell in SlickEdit (my usual location that helps discover things like the need for fflush(stdout) and such) I get a blank area where the text should be. To me this would tell me that I might be missing something. When I run it in a cmd shell this does not happen. [edit]Ah the CR of the CR-LF was erasing the line for the SlickEdit output window. A fallout of binary mode reading.[/edit]

I am using Windows, so I might suspect my version would have issues with the binary vs text modes. But I wouldn't expect such things on Solaris. Outputting one at a time from 0 to size would have been my suggestion, so I'm a little miffed.

What is the "big picture" of what you are trying to do?


[edit2]This is the code I am using.
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::ios;
  5. #include <fstream>
  6. using std::ifstream;
  7.  
  8. const char filename[] = "file.txt";
  9.  
  10. int main ()
  11. {
  12. ifstream file(filename, ios::in | ios::binary | ios::ate);
  13. long size = file.tellg();
  14. file.seekg(0, ios::beg);
  15. char *buffer = new char [size];
  16. file.read(buffer, size);
  17. file.close();
  18. for ( long i = 0; i < size; ++i )
  19. {
  20. cout << buffer[i];
  21. }
  22. cout << endl;
  23. delete[] buffer;
  24. return 0;
  25. }
  26.  
  27. /* file.txt (no newline at end)
  28. SAM LOVES TO TEST FILES
  29. */
  30.  
  31. /* my output
  32. SAM LOVES TO TEST FILES
  33. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #7
Jul 14th, 2005
The big picture is that I am loading an MQ series queue with the text of a file. (It's a command line utility). Since I don't know the size of the message at the start, I am reading it in binary so to that I can set the size of the buffer dynamically. In any case, I was able to get it to work with the C code version, so I'm good to go.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #8
Jul 14th, 2005
A vector of strings for C++?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading in file in binary mode

 
0
  #9
Jul 14th, 2005
I don't need a vector, I have a single string. (The file contains one message, not a bunch of messages)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading in file in binary mode

 
0
  #10
Jul 14th, 2005
Another stray thought:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. int main ()
  6. {
  7. static const char filename[] = "file.txt";
  8. std::ifstream file(filename);
  9. std::ostringstream text;
  10. text << file.rdbuf(); // slurp
  11. std::cout << text.str() << std::endl;
  12. return 0;
  13. }
  14.  
  15. /* my output
  16. SAM LOVES TO TEST FILES
  17. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC