943,831 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7797
  • C++ RSS
Jul 27th, 2004
0

Text File Input and manipulation

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PViper is offline Offline
3 posts
since Jul 2004
Jul 27th, 2004
0

Re: Text File Input and manipulation

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 ->
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 27th, 2004
0

Re: Text File Input and manipulation

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!).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PViper is offline Offline
3 posts
since Jul 2004
Jul 27th, 2004
0

Re: Text File Input and manipulation

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
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 27th, 2004
0

Re: Text File Input and manipulation

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jul 27th, 2004
0

Re: Text File Input and manipulation

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PViper is offline Offline
3 posts
since Jul 2004

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: Amibguity in C & C++
Next Thread in C++ Forum Timeline: Window Console Help





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


Follow us on Twitter


© 2011 DaniWeb® LLC