Question:
Reading a file in C++?
Robbo
2008-09-09 21:44:08 UTC
Suppose I want to read a file into a program using C++, use the bytes and chop them up into single bits... how can this be done?
The thing is the files can be of any type - regardless of whether it's a text file, an image, or anything else. Just something that would generate a bunch of bits and bytes.

Thanks
Five answers:
2008-09-09 21:48:43 UTC
1. Include the necessary headers.



#include

using namespace std;



2. Declare an input file stream (ifstream) variable. For example,



ifstream inFile;



3. Open the file stream. Path names in MS Windows use backslashes (\). Because the backslash is also the string escape character, it must be doubled. If the full path is not given, most systems will look in the directory that contains the object program. For example,



inFile.open("C:\\temp\\datafile.txt");



4. Check that the file was opened. For example, the open fails if the file doesn't exist, or if it can't be read because another program is writing it. A failure can be detected with code like that below using the ! (logical not) operator:



if (!inFile) {

cerr << "Unable to open file datafile.txt";

exit(1); // call system to stop

}



5. Read from the stream in the same way as cin. For example,



while (inFile >> x) {

sum = sum + x;

}



6. Close the input stream. Closing is essential for output streams to be sure all information has been written to the disk, but is also good practice for input streams to release system resources and make the file available for other programs that might need to write it.



inFile.close();



Example

The following program reads integers from a file and prints their sum.



// io/read-file-sum.cpp - Read integers from file and print sum.

// Fred Swartz 2003-08-20



#include

#include

#include

using namespace std;



int main() {

int sum = 0;

int x;

ifstream inFile;



inFile.open("test.txt");

if (!inFile) {

cout << "Unable to open file";

exit(1); // terminate with error

}



while (inFile >> x) {

sum = sum + x;

}



inFile.close();

cout << "Sum = " << sum << endl;

return 0;

}
jplatt39
2008-09-09 23:54:11 UTC
To comment on neptunecentury's excellent answwer which Yahoo tells me it won't save a thumbs up for, read and write are unformatted data operations you can do with any sorts of streams -- files, something that came in over the network or whatever. Here is a page from C++.com that talks about it:



http://www.cplusplus.com/reference/iostream/istream/read.html



There are also read and write functions in cstdio, which means they are C functions, and I would probably write this as a C program but that's partly because without namespaces the memory overhead is lower and partly because, having learned what I know of C++ after working with C I'm biased.
neptunecentury
2008-09-09 22:01:52 UTC
FILE FP1;

BYTE Buf[2048];



FP1 = fopen("file", "r");

if (FP1 == 0) return;



fread(Buf, sizeof(BYTE), 2048, FP1);



fclose(FP1);





Buf now contains an array of 2048 bytes that have been read from the file. You can adjust the size of the array to suit your needs
Joseph K
2008-09-09 21:48:07 UTC
There is, to my knowledge, no way to see individual bits and bytes via C++, since it's not a base level programming language. You might want to try using something like FORTRAN or BASIC.
?
2016-05-22 12:29:45 UTC
You try, we will correct it !!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...