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 << 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!

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 ->

// Pseudocode-like only
int indexOfOpening = 0;

for (i=0; i<lengthOfLine; i++)
{
    if(ch[i]=='(')
    {
        indexOfOpening = i;
        i = lengthOfLine;
    }
}

[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. ;)

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!).

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 ;)

Try the fstream tutorial here at Daniweb.You should find it useful.Covers a lot of stuff related to file i/o. :D
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

//stuff needed here ;-)

while(!file.eof())
{
    file.getline(txt_buffer,80);
    no_of_line++;

    if(no_of_line == 10) 
    {
        cout<<txt_buffer;
        break;
    }
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.