Help with C++ files?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster

Help with C++ files?

 
0
  #1
Oct 20th, 2009
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
  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
  1. for
or
  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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 304
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 31
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz
 
2
  #2
Oct 21st, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #3
Oct 21st, 2009
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
  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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #4
Oct 21st, 2009
Thanks for the complete code! A few modifications and it worked great!
Reply With Quote Quick reply to this message  
Reply

Tags
c++, file, store

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC