I want to open a file for reading, the C++ way. I need to be able to do it for:
-
text files, which would involve some sort of read line function.
-
binary files, which would provide a way to read raw data into a
char*buffer.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are three ways to do this, depending on your needs. You could use the old-school C way and call
fopen/fread/fclose, or you could use the C++ fstream facilities (ifstream/ofstream), or if you’re using MFC, use theCFileclass, which provides functions to accomplish actual file operations.All of these are suitable for both text and binary, though none have a specific readline functionality. What you’d most likely do instead in that case is use the fstream classes (fstream.h) and use the stream operators (<< and >>) or the read function to read/write blocks of text:
Note that, if you’re using Visual Studio 2005 or higher, traditional fstream may not be available (there’s a new Microsoft implementation, which is slightly different, but accomplishes the same thing).