| | |
Error reading in file in binary mode
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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)
<< 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
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)
C++ Syntax (Toggle Plain Text)
// reading binary file #include <iostream.h> #include <fstream.h> const char * filename = "example.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 << "the complete file is in a buffer"; delete[] buffer; return 0; }
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
I didn't see the issue, but WRT a C version I think it would be something like this.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main(void) { static const char filename[] = "file.txt"; FILE *file = fopen(filename, "rb"); if ( file ) { char *buffer; long size; fseek(file, 0, SEEK_END); size = ftell(file); rewind(file); buffer = malloc(size); if ( buffer ) { if ( fread(buffer, size, 1, file) == 1 ) { /* puts(buffer); */ } } fclose(file); free(buffer); } return 0; }
"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
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.
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
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.....)
// 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.....)
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.
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::endl; using std::ios; #include <fstream> using std::ifstream; const char filename[] = "file.txt"; int main () { ifstream file(filename, ios::in | ios::binary | ios::ate); long size = file.tellg(); file.seekg(0, ios::beg); char *buffer = new char [size]; file.read(buffer, size); file.close(); for ( long i = 0; i < size; ++i ) { cout << buffer[i]; } cout << endl; delete[] buffer; return 0; } /* file.txt (no newline at end) SAM LOVES TO TEST FILES */ /* my output SAM LOVES TO TEST FILES */
"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
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.
Another stray thought:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <sstream> int main () { static const char filename[] = "file.txt"; std::ifstream file(filename); std::ostringstream text; text << file.rdbuf(); // slurp std::cout << text.str() << std::endl; return 0; } /* my output SAM LOVES TO TEST FILES */
"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
![]() |
Other Threads in the C++ Forum
- Previous Thread: int to vector<>::iterator
- Next Thread: View all running processes, C++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






