command line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;
ifstream infile;

int main(int argc, char* argv[])
{
  infile.open("linedata");


  int numb_char=0;
  char letter;

  while(!infile.eof())
    {
      infile.get(letter);
      cout << letter;
      numb_char++;
      break;
    }

  cout << " the number of characters is :" << numb_char << endl;
  infile.close();

  return 0;
}

why is not getting right data
g++ line3.cpp
./a.out a <linedata
t the number of characters is :1
//for some reason is getting the t of ./a.out why is not getting "a"
input
linedata is:
this is just an example of many A aa Aa
it suppose to be
a the number of characters is:6
thanks
line 17: break;

./a.out a <linedata
You are passing as 'a' as a parameter, and redirecting the standard input to the file linedata. However your program does not use its parameters, and does not read from standard input.

1
2
while( not infile.eof() ){
  infile.get(letter);
You don't reach eof until you try to read it. So it will be too late.
Change it to while( infile.get(letter) ){
Topic archived. No new replies allowed.