Requirements to create and access information stored in a text file is accomplished by using file streams in c++.
Include the following headers into your program:
[codesyntax lang=”cpp”]
#include <iostream> #include <fstream>
[/codesyntax]
Prepare the text file for output:
[codesyntax lang=”cpp”]
std::ofstream sw("FileSpecHere"); // Comment about the file
[/codesyntax]
The output is accomplished by using the following command:
[codesyntax lang=”cpp”]
sw << "pstmt->setInt64(" << ij << ", tab_" << ut.mda[ij].tabName << "." << ut.mda[ij].colName << ");" << endl;
[/codesyntax]
Notice the “<<” tells the operating system to send the proceeding contents to the file stream. Also, notice the endl statement must be issued signaling the end of the line.
Prepare the file for input by issuing the following command:
[codesyntax lang=”cpp”]
ifstream sw("FileSpecHere");
[/codesyntax]
To loop through the input file’s content issue the following command:
[codesyntax lang=”cpp”]
string buf; ifstream sw("FileSpecHere"); while (!sw.eof()) { sw >> buf; }
[/codesyntax]
Notice the >> statement signals the operating system to place the next line in the text file into the variable buf.
Of course, then the file transmission is complete the following close statement must be issued:
[codesyntax lang=”cpp”]
sw.close();
[/codesyntax]
By issuing these command input and output to text files is easily accomplished.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.