5

I have a phone.txt like:

09236235965
09236238566
09238434444
09202645965
09236284567
09236235965
..and so on..

How can I process this data line by line in C++ and add it to a variable.

string phonenum;

I know I have to open the file, but after doing so, what is done to access the next line of the file?

ofstream myfile;
myfile.open ("phone.txt");

and also about the variable, the process will be looped, it will make the phonenum variable the current line its processing from the phone.txt.

Like if the first line is read phonenum is the first line, process everything and loop; now the phonenum is the 2nd line, process everything and loop until the end of the last line of the file.

Please help. I'm really new to C++. Thanks.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
user1553142
  • 237
  • 1
  • 11
  • 21
  • 4
    See [`std::getline()`](http://en.cppreference.com/w/cpp/string/basic_string/getline). – hmjd Nov 23 '12 at 17:01

3 Answers3

6

Read the comments inline please. They will explain what is going on to assist you in learning how this works (hopefully):

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main(int argc, char *argv[])
{
    // open the file if present, in read mode.
    std::ifstream fs("phone.txt");
    if (fs.is_open())
    {
        // variable used to extract strings one by one.
        std::string phonenum;

        // extract a string from the input, skipping whitespace
        //  including newlines, tabs, form-feeds, etc. when this
        //  no longer works (eof or bad file, take your pick) the
        //  expression will return false
        while (fs >> phonenum)
        {
            // use your phonenum string here.
            std::cout << phonenum << '\n';
        }

        // close the file.
        fs.close();
    }

    return EXIT_SUCCESS;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • hello, this is really great. anyways, is this correct? ifstream fs("D:\Read File Test\phone.txt"); – user1553142 Nov 23 '12 at 17:20
  • Anyways one last thing. I tried the code, and it does the process 3 times even though I only have 2 lines on the text file. Why is that? – user1553142 Nov 23 '12 at 17:48
  • @user1553142 It should not. I've cut/paste your sample data to a text file and it performs as expected. one phonenum per entry. If you're using a debugger you might want to examine the content of phonenum and see what is present on either the first, or last, iteration. – WhozCraig Nov 23 '12 at 19:00
3

Simple. First, note that you want an ifstream, not an ofstream. When you're reading from a file, you're using it as input - hence the i in ifstream. You then want to loop, using std::getline to fetch a line from the file and process it:

std::ifstream file("phone.txt");
std::string phonenum;
while (std::getline(file, phonenum)) {
  // Process phonenum here
  std::cout << phonenum << std::endl; // Print the phone number out, for example
}

The reason why std::getline is the while loop condition is because it checks the status of the stream. If std::getline fails in anyway (at the end of your file, for example), the loop will end.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

You can do that :

 #include <fstream>
 using namespace std;

 ifstream input("phone.txt");

for( string line; getline( input, line ); )
{
  //code
}
Mils
  • 388
  • 6
  • 22
  • For some reason there has been a lot of `using namespace std;` around here lately: http://www.parashift.com/c++-faq/using-namespace-std.html Also, why is `string line;` there twice? And you don't `#include `. – BoBTFish Nov 23 '12 at 17:11