Luckily for you, the c++ standard provides you with the tools necessary to open, read, and write to files. You can use these tools by including the <fstream> library. Then you can create ifstream objects that will allow you to open and read from a file, and ofstream objects that will allow you to write to a file.
Here is a pretty decent tutorial for opening, reading and writing to text files using the <fstream> library:
http://www.cplusplus.com/doc/tutorial/files/
So, you wish to open a file and examine it's contents in 5 character increments. Since we will not be writing to file, all we will need is a 'ifstream' object.
Probably the easiest method I can think of will involve opening a file, reading its entire contents into a single <string> object, and then performing the desired operations on that string.
I'm not sure what your data will look like, so I will have to make some assumptions with this code, but feel free to modify it as you like.
Here is the pseudo code for what I am about to demonstrate:
1. create an 'ifstream' object (derived from <fstream>)
2. attempt to open an existing .txt file.
3. perform error checking (check to see if file opened correctly)
4. read the entire .txt file into a single 'string' object.
5. perform desired operations on string object (in 5 char increments)
6. close the ifstream object
Now let's translate pseudo code into c++ code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string document;
// 1. create an 'ifstream' object
ifstream infile;
// 2. attempt to open an existing .txt file.
infile.open("c:\\documents\\data.txt");
// 3. perform some error checking
if(! infile.open())
{
cout << "\a Unable to open file ! ";
cout << "\n Press [Enter] to continue...";
cin.get();
}
// 4. read the entire file into a single 'string' object.
string temp;
while (! infile.eof())
{
infile >> temp;
document += temp;
}
// 5. perform desired operations on string object
//extract the first 5 chars from the 'document' string
temp.clear();
for(int i=0; i<5; i++)
{
temp += document[i];
}
// 'temp' is now available for testing
// 6. close the ifstream object to free up system resources
infile.close();
return 0;
}
I do not have a compiler on me' old laptop, so if anyone sees any mistakes, or has a better suggestion, please feel free to offer it up.
One suggestion: Once you feel comfortable with this code, try pushing the .txt document into a <vector> class object as opposed to a <string>.