| | |
Help with C++ files?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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 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 or loop. If someone could come up with a sample script or snippet, that would be great.
Any help would be greatly appriciated! Thanks!
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)
char[5]
C++ Syntax (Toggle Plain Text)
for
C++ Syntax (Toggle Plain Text)
while
Any help would be greatly appriciated! Thanks!
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:
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>.
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)
#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>.
Last edited by Clinton Portis; Oct 21st, 2009 at 1:22 am. Reason: i make misteaks
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
You can search the forum for many discussions on why the eof( ) method of loop control is not advised.
Make your array size 6 to allow for the null terminator at the end of a string, then
C++ Syntax (Toggle Plain Text)
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.
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- php form upload video files not working (PHP)
- hjsplit doenst work for avi files? (Windows NT / 2000 / XP)
- News Story: Royal Air Farce as RAF loses high security vetting files (Network Security)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- Does Samba send deleted files to a recycle bin? (*nix Software)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP)
- FTP files (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Converting int into months
- Next Thread: Compiling linux based static library in Windows
| Thread Tools | Search this Thread |
.net ada api array arrays background based binary bitmap bmp c# c++ calculator change char char* class code compression console decide desktop directshow download ebook embed encryption engine error examples file floatingpoint fstream function functions game gmail graph guessing gui handling htaccess ifstream image input insert int java javascript keyboard linux list math matrix modal numbers open output parallel parameter php primenumbersinrange problem process professor program programing programmer programming projects python qt read reading recursion recursive regqueryvalueex remote space stop stream string strings studio subclass temperature template templates test text timer toolkit tree university url variable visual web win32 write







