Hi,

I have a txt file that contain certain data inside it.
Example of the txt file:
Name=John Age=20 Address=c-34, Newyork
Name=Martin Age=23 Address=123, New Delhi
....
....
and so on.....
Now I have to process the each line get from the file and put the values into arrays.
arr_name, arr_age, arr_add will be filled from the file.
I have to read 1 line at a time, pass the values of arr_name, arr_age, arr_add to the other function to process it. Again back to the file processing function and read the second line, pass it to the function and similarly for other lines of the file.
How can I complete this task.

Thanks in advance.
Parvez

There are several ways to do it, but one way is: in a loop call getline() to read each line, then for each line read use stringstream object to split it into individual tokens. Post the code you have attempted and we can help you some more.

Ok here is the code that I want to manipulate:

#include<iostream>
#include<fstream>

int readfile()
{
    ifstream myfile;
    string str_line;
    int flag=0;  // used to check if the file already open or not

    if ( flag ==0)   // initially file is not open so open it here
    {
        myfile.open(file.txt);
        flag=1;    // set the flag that file is opened now

    }


    if (myfile.is_open())
    {
            cout<<"File Opening Success";
            flag=1;
            getline(myfile, str_line);  // get 1 line at a time. After getting this one line, I want to pass this 1 line data to another process and
            // get back to here again for the next line to process. without opening the file again

            cout<<str_line;
            if(myfile.eof())   // if eof reached close the file.
            {
                myfile.close();
                flag = -1;
                exit;
            }
    }
    else
    {
        TRACE("File Opening NOT Success");
        flag = -1;
        exit;

    }

return 0;
}

Problem with this code is, as soon as I loop back to this function(readfiel), it not keep opening the file. and the condition check is file open() fail.

In short my problem is, I want to porcess the file line by line and each time I read a line, send it to another process to and after processing the line come back to this function and read the next line.

Thanks in advance for your help and understanding.

You can't call that function in a loop to read the file line-by-line because the file pointer is destroyed as soon as the function returns to it's caller. You need to call that function only once, then have that function read each line in a loop. You will most likely have to reorganize the way the rest of your program works. You didn't post it so I don't know what it does.

The check for file open on line 10 doesn't work they way you want it to because the file is automatically closed when the function exits. The flag on line 8 therefore becomes useless. What that function should do is something like this:

open the file
start of loop
   read a line
   exit this loop if end-of-file reached
   process the line
   go back to start of loop
end of loop

It can be easily coded like this:

ifstream infile("myfile.txt");
std::string line;

if( infile.is_open() )
{
    while( getline(infile,line) )
    {
      // do something here
    }
}

Thanks for your responce Ancient Dragon :)

I think, I was not clear enough to explain my problem.

Forget about the wile loop for time being.
My problem is: I need to read a line from the input file using some readLine() function. As soon as I exit from this readLine() function my open file will be closed . Then pass this line to next function let say fun1() for some operation.

Again come back to the readLine() function and need to read the next line from it and pass that line to next function fun1(). Need to do this process till the end of the file reached i.e. all the lines from the file read.

Can I use function like seekg() and tellp() and use some global varibale to keep track of the line being read?

A quick responce from you guys will be highly appreciated.

Thanks in advance.

If you don't want to do it all inside that function then you have to pass a pointer to the function.

#include<iostream>
#include<fstream>

ifstream& readfile(ifstream& myfile)
{
    if( getline(myfile, str_line) )
    {
       cout<<str_line;
    }
    return myfile;
}

int main()
{
   ifstream myfile("myfile.txt");
   if( myfile.is_open() )
   { 
       while( readfile(myfile) )
       {
          // do something
       }
    }
}

But why go to all the trouble to call readline() when you could call getline() easier directly within the while loop? No one in his right mind would attempt to code it the way you described -- too complicated and just too damned sloooow.

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.