Hi, Im new to DaniWeb and C++ and was hoping some of you more experienced tech fellas could help me out with a problem.

I want to open a file, then read it by 5 characters at a time by putting it into a

char[5]

type of array, then analyze those 5 characters (like go to a function if one of the characters was a '<'). When thoes 5 characters have been read with a

for

or

while

loop. If someone could come up with a sample script or snippet, that would be great.

Any help would be greatly appriciated! Thanks!

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>.

commented: good reply +21

To read just 5 characters at a time to your array, you could use getline( ).

Make your array size 6 to allow for the null terminator at the end of a string, then

char arr[6];
//open your filestream as shown in previous posting
while( infile.getline( arr, 6 ) )  //reads up to 5 char or newline, whichever first
{
    //process the data
}

You can search the forum for many discussions on why the eof( ) method of loop control is not advised.

Thanks for the complete code! A few modifications and it worked great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.