Question:
I like C++ but reading and writing to text files is troublesome compared to other languages?
2010-10-19 18:32:23 UTC
is there a class or code to shield me from dealing with the details of reading and writing files?
Four answers:
Cubbi
2010-10-20 06:02:03 UTC
C++ can treat files at levels of abstraction much higher than most other languages. You can represent the file as a standard C++ iterator pair, or as a range, and apply every algorithm to it, never even explicitly "reading" anything, in your program.



If you don't mind using or re-implementing a subset of boost, you can transform the file through compression algorithms, filters, and regexps with boost.iostreams, you can apply a whole BNF grammar to it, converting it into a fusion container or into an AST with a single line of code in boost.spirit.



Just because C++ allows low-level routines doesn't mean it's always meaningful to use them.
Shadow Wolf
2010-10-20 02:21:43 UTC
The problem with reading files is that each file is different. Reading it has to be customized to the task or purpose of reading the file. This doesn't mean that you can't hide some of the tedious work in a custom class.



Since you are using C++, you can create a class that will help you. Your class will likely evolve over time, but you can use it to hide some of the mundane details.



You know you need to open a file. So the constructor might include opening the file and checking to be sure it was opened. Be sure the destructor closes and/or releases any file pointers and memory that you use when an object is created.



You can include different types of file I/O. You may want to make this part of the constructor set that opens the file. Use a structured interface that may stay the same outside of the class but can behave differently internally depending on the file type opened.



Be sure to use a structured approach to creating files. This makes reading them a bit easier. You have to be a bit more organized in the design but it makes writing the associated code easier.



Think about data separators. Commas , white space or other special characters may be used. End of lime terminators are also important depending on what type of line terminator you are dealing with. You may want to include terminator options.



You'll also want to consider different types of file I/O and how to handle them. Since you'll have a class object, you can hide all sorts of class details that change how your class works.



An example would be a file system I created for reading the equivalent of BASIC random access files for C. Instead of dealing with the work of creating new file handler, I created a set of functions that would deal with a file as a set of fixed length records. I only had to customize how each record was handled which hid most of the work of reading the file. You can do the same in C++ with several twists that are not as easy to do in C.



No matter what you do, there will be work involved with file I/O. Design your class so that you don't need to do as much work. As your class evolves, remember to take advantage of C++ features so that you don't break older code that uses your class.



Don't forget to take advantage of existing classes and functions to avoid re-inventing the wheel. Creating the file I/O class may actually change how you look at file I/O. You'll have to look at it much more in depth to create a good class that will actually help you.



Shadow Wolf
JoelKatz
2010-10-20 01:34:30 UTC
Can you be more specific about what issues you're having? Are you trying to read and write lines? Are you trying to insert data into the middle of a file? Are you trying to store binary data? Are you trying to index and lookup information by key?



Update: Reading text files? Reading binary files? What do you mean by parsing? Do you mean XML? Or lines? It's really not clear what you want.



Update: iostream makes reading text files really simple. How much more than a function to read a line do you need?
oops
2010-10-20 01:50:49 UTC
iostream

boost iostreams

boost spirit

boost conversion



Give an example of what you are having trouble doing. If it's just "reading text files", the standard library has no trouble doing that at all, watch:



// Opens a file called input.txt, and puts each separate word

// as a separate item in to a vector



#include

#include

#include

#include



using namespace std;



int main()

{

    string temp;

    vector vs;

    ifstream fin("input.txt");

    fin >> temp;

    while(fin)

    {

        vs.push_back(temp);

        fin >> temp;

    }

}


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