EOF with getline?

Hi folks, I am trying to read until the end of file with the function cin.getline()

I tried

cin.getline(mensaje,200,'EOF');

But it won't work, it stops when it finds a \n .

I should say that the txt is passed as a parameter in the program when executing it
./program < blahblah

the "blahblah" is what I am trying to read until the EOF.
I should say that the txt is passed as a parameter in the program when executing it
./program < blahblah

This tells me you may be going about this wrong. Do you mean that you are trying to grab the arguments passed to your application when it is started from the command line?
No no, since if you do the
< file

with file being a text file, cin,getline will read the text file (well, actually its a redirection to the standard input)
and i want to read it ALL, because with cin.getline it will stop reading when it founds a \n.
Before I go off on a rant, we should establish what platform you are writing on.
linux, lol
EOF in an integer type .
cin.getline() stops at the character designated by third argument . In your case it does recognize char EOF so stops at '\n' ( which is default)

trying using
while(!cin.eof())
{
}

OR
while(cin.get() != EOF)

{

}

OR
while(!cin.getline(mensaje,200).eof())
{

}
Last edited on
I'm glad I asked. Although I think it is simular enough to Windows for that not to be an issue, I can not claim to know enough for my input to matter.
I tried this
1
2
3
4
while(cin.get() != EOF)
{
cin.getline(mensaje,200);
} 


but it becomes an inifinite loop it seems (or atleast redirecting with < won't work and ask you to input something more)
Play around with this to get an idea of how things work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  size_t n = 0;
  string s;
  while (getline( cin, s ))
    {
    cout << n++ << ": " << s << endl;
    }
  cout << n << ": " << s << endl;  // last line
  return 0;
  }

D:\prog\foo> echo Hello world | a
0: Hello world
1:

D:\prog\foo> 
D:\prog\foo> a < a.cpp
0: #include <iostream>
1: #include <string>
...
13:    }
14:

D:\prog\foo> 

Hope this helps.
Seems it will be useful, I'll try it out tomorrow since I must go to sleep now, thanks ;)
Topic archived. No new replies allowed.