944,179 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1045
  • C++ RSS
Oct 20th, 2009
0

Help with C++ files?

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. 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
C++ Syntax (Toggle Plain Text)
  1. for
or
C++ Syntax (Toggle Plain Text)
  1. while
loop. If someone could come up with a sample script or snippet, that would be great.

Any help would be greatly appriciated! Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Oct 21st, 2009
2
Re: Help with C++ files?
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string document;
  9.  
  10. // 1. create an 'ifstream' object
  11. ifstream infile;
  12.  
  13. // 2. attempt to open an existing .txt file.
  14. infile.open("c:\\documents\\data.txt");
  15.  
  16. // 3. perform some error checking
  17. if(! infile.open())
  18. {
  19. cout << "\a Unable to open file ! ";
  20. cout << "\n Press [Enter] to continue...";
  21. cin.get();
  22. }
  23.  
  24. // 4. read the entire file into a single 'string' object.
  25. string temp;
  26. while (! infile.eof())
  27. {
  28. infile >> temp;
  29. document += temp;
  30. }
  31.  
  32. // 5. perform desired operations on string object
  33. //extract the first 5 chars from the 'document' string
  34.  
  35. temp.clear();
  36. for(int i=0; i<5; i++)
  37. {
  38. temp += document[i];
  39. }
  40.  
  41. // 'temp' is now available for testing
  42.  
  43. // 6. close the ifstream object to free up system resources
  44. infile.close();
  45.  
  46. return 0;
  47. }

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>.
Last edited by Clinton Portis; Oct 21st, 2009 at 1:22 am. Reason: i make misteaks
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 21st, 2009
0
Re: Help with C++ files?
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
C++ Syntax (Toggle Plain Text)
  1. char arr[6];
  2. //open your filestream as shown in previous posting
  3. while( infile.getline( arr, 6 ) ) //reads up to 5 char or newline, whichever first
  4. {
  5. //process the data
  6. }

You can search the forum for many discussions on why the eof( ) method of loop control is not advised.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 21st, 2009
0
Re: Help with C++ files?
Thanks for the complete code! A few modifications and it worked great!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Converting int into months
Next Thread in C++ Forum Timeline: Compiling linux based static library in Windows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC