Reading a .txt file in C++ [on hold]


Reading a .txt file in C++ [on hold]



So im trying to write a program in C++ that takes input line by line from a text file and give it as and output but im running into errors that I cant find very good answers to. Thanks in advance for the help



Code is:


1 #include "stdafx.h"
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 using namespace std;
6 int main()
7 {
8 string line;
9 ifstream file("INPUT.txt");
10 if (file.is_open())
11 {
12 while(getline(file,line)
13 {
14 cout << line << 'n'
15 }
16 file.close();
17
18 }
19 else
20 {
21 cout << "file is not open" << 'n';
22 }
23
24 return 0;
25 }



The errors from the compiler read:



line(15): error C2064: term does not evaluate to a function taking 1 arguments



and



line(16): error C2146: syntax error: missing ')' before identifier 'file'



This question appears to be off-topic. The users who voted to close gave this specific reason:





missing a semicolon end of line 14
– Martin Beckett
Jun 30 at 3:32






ps useful error messages have never been a big part of c++
– Martin Beckett
Jun 30 at 3:34





Especially with the VS compiler. gcc does quite a bit better job, but does suffer from the wall-to-wall dump of template, trait and class information.
– David C. Rankin
Jun 30 at 3:49




1 Answer
1



line 12 is missing a closing parentheses, should be:
while(getline(file,line))


while(getline(file,line))



line 14 is missing a semicolon, should be:
cout << line << 'n';


cout << line << 'n';



Final:


~/tmp/junk > cat a.cc
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file("INPUT.txt");
if (file.is_open())
{
while(getline(file,line))
{
cout << line << 'n';
}
file.close();
}
else
{
cout << "file is not open" << 'n';
}
return 0;
}

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV