954,113 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with C++ files?

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!

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

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 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 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 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 )
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 class object as opposed to a .

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

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.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

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

dylank
Junior Poster in Training
66 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: