Text File Input and manipulation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 3
Reputation: PViper is an unknown quantity at this point 
Solved Threads: 0
PViper PViper is offline Offline
Newbie Poster

Text File Input and manipulation

 
0
  #1
Jul 27th, 2004
Hi, I'm trying to write a program(beginner level) to read a text file line by line and compare each character with one in my code. So far I have read the lines from the file:

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
vector<string> v;
ifstream in("textfile.txt");
string line;
while(getline(in, line))
v.push_back(line); // Add the line to the end
// Add line numbers:
for(int i = 0; i < v.size(); i++)
cout << i << ": " << v[i] << endl;
}

and added line numbers, but now I would like to compare the individual charatcters in each line.

For example, line 10 is : "This is line ten (10)"
How do I check that the number inseide the brackets is "10" and let the sentence print if true? ( and repeat for all lines)

Appreciate any help, thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Text File Input and manipulation

 
0
  #2
Jul 27th, 2004
Greetings.
Umm, assuming that the lines in your file always have ().
And, you would like to get the content in the bracket then you could do this:-

[1] Get the content of line 10, store them as an array of characters.
[2] Make a for-loop, compare each character in the line ->
  1. // Pseudocode-like only
  2. int indexOfOpening = 0;
  3.  
  4. for (i=0; i<lengthOfLine; i++)
  5. {
  6. if(ch[i]=='(')
  7. {
  8. indexOfOpening = i;
  9. i = lengthOfLine;
  10. }
  11. }
[3] Now, you have the index of the opening bracket.
[4] Get the index of the starting element inside the bracket which is simple what you
got in [3] plus 1.
[5] Make another for-loop to gather every other character until you see a ')'.
[6] After this, you would consider converting the elements you've collected into an
integer, or anything relevant.
Hope this would help.
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: PViper is an unknown quantity at this point 
Solved Threads: 0
PViper PViper is offline Offline
Newbie Poster

Re: Text File Input and manipulation

 
0
  #3
Jul 27th, 2004
Thanks,

I try that...

Actually, the brackets was just an example(bad one).

I'm trying just to learn about sequential input and manipulation.
What i'm trying to do is input a textfile containing lines of characters, integers and other symbols, and outputting the result in different textfiles depending on whether the line contains integers, characters, others, or all.

I'm trying to work it out bit by bit, hence the bracket example.(not finding it easy!).
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Text File Input and manipulation

 
0
  #4
Jul 27th, 2004
Greetings.
Oh alright.
Umm, I don't know what else to advise as it would be better to explain with examples/scenarios.
You try it out and feel free to post your codes here when you encounter any problems and I'll be sure to help
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Text File Input and manipulation

 
0
  #5
Jul 27th, 2004
Try the fstream tutorial here at Daniweb.You should find it useful.Covers a lot of stuff related to file i/o.
http://www.daniweb.com/techtalkforums/thread6542.html

Btw,you dont really have to store a number at line 10 to say it's line 10.Try this

  1. //stuff needed here ;-)
  2.  
  3. while(!file.eof())
  4. {
  5. file.getline(txt_buffer,80);
  6. no_of_line++;
  7.  
  8. if(no_of_line == 10)
  9. {
  10. cout<<txt_buffer;
  11. break;
  12. }
  13. }
  14.  
  15. file.close();

Agreed it's a bit slow and takes too many readings,but it's a better way to read lines line to store within the file the line number.

There is a better way,using structs or classes which is discussed in the tutorial
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: PViper is an unknown quantity at this point 
Solved Threads: 0
PViper PViper is offline Offline
Newbie Poster

Re: Text File Input and manipulation

 
0
  #6
Jul 27th, 2004
Thanks to all replies to this post.

I will be trying all your recommendations.

What I'm doing is just trying to learn sequential reading and manipulation.

I have a textfile consisting of lines containg characters, integers and other symbols(operators).

I want my program to read each line, decide if it contains chars, ints, others, or all, and send the line to chars.txt, ints.txt, others.txt, or all.txt accordingly.

many thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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