5

I am using STL. I need to read lines from a text file. How to read lines till the first \n but not till the first ' ' (space)?

For example, my text file contains:

Hello world
Hey there

If I write like this:

ifstream file("FileWithGreetings.txt");
string str("");
file >> str;

then str will contain only "Hello" but I need "Hello world" (till the first \n).

I thought I could use the method getline() but it demands to specify the number of symbols to be read. In my case, I do not know how many symbols I should read.

Vladimir
  • 117
  • 1
  • 1
  • 9

4 Answers4

10

You can use getline:

#include <string>
#include <iostream>

int main() {
   std::string line;
   if (getline(std::cin,line)) {
      // line is the whole line
   }
}
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
2

using getline function is one option.

or

getc to read each char with a do-while loop

if the file consists of numbers, this would be a better way to read.

do {
    int item=0, pos=0;
    c = getc(in);
    while((c >= '0') && (c <= '9')) {
      item *=10;
      item += int(c)-int('0');
      c = getc(in);
      pos++;
    } 
    if(pos) list.push_back(item);
  }while(c != '\n' && !feof(in));

try by modifying this method if your file consists of strings..

user322
  • 103
  • 8
2

Thanks to all of the people who answered me. I made new code for my program, which works:

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

using namespace std;

int main(int argc, char** argv)
{
    ifstream ifile(argv[1]);

    // ...

    while (!ifile.eof())
    {
        string line("");
        if (getline(ifile, line))
        {
            // the line is a whole line
        }

        // ...
    }

    ifile.close();

    return 0;
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Vladimir
  • 117
  • 1
  • 1
  • 9
  • 1. It is better to state the condition just as `while (ifile)`, since `ifstream` has overloaded `operator bool()` to test whether it is ok or not. 2. Do not close `ifile` explicitly. This is done implicitly in `ifstream` destructor according to RAII principle. – Mikhail Apr 24 '16 at 16:22
  • 3. Do not specify "" value for `std::string` - it is just redundant, string is initialized with "" in default constructor. 4. You have two tests here: in `while` and in `if`, while on one is enough, which is `getline`. Move it to `while` condition and you won't need to test `ifile` at all. – Mikhail Apr 24 '16 at 16:25
  • [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/) Change `while (!ifile.eof()) { ... getline(ifile, ...) ... }` to `while (getline(ifile, ...)) { ... }` – Remy Lebeau May 20 '22 at 22:47
1

I suggest:

#include<fstream>

ifstream reader([filename], [ifstream::in or std::ios_base::in);

if(ifstream){ // confirm stream is in a good state
   while(!reader.eof()){
     reader.read(std::string, size_t how_long?);
     // Then process the std::string as described below
   }
}

For the std::string, any variable name will do, and for how long, whatever you feel appropriate or use std::getline as above.

To process the line, just use an iterator on the std::string:

std::string::iterator begin() & std::string::iterator end() 

and process the iterator pointer character by character until you have the \n and ' ' you are looking for.

user633658
  • 2,463
  • 2
  • 18
  • 16
  • [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/) Change `while(!reader.eof()){ reader.read(...); ...}` to `while (reader.read(...)){ ... }` – Remy Lebeau May 20 '22 at 22:50