Reputation: 3
I tried to write a little program that retrieves data from a .txt file and shows it in the terminal but I got an error. I have to say I am new to visual studio; until now i worked on code:blocks
I have tried the suggestion from the error code, adding the #include "pch.h" at the beggining but it still didn t work.
The error code is C1010 (if i build the code without line #include "pch.h"); If i build it with that line i recieve multiple error codes:
"1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2065: 'ifstream': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2146: syntax error: missing ';' before identifier 'inFile'
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(10): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): warning C4129: 'B': unrecognized character escape sequence
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(11): warning C4129: 'D': unrecognized character escape sequence
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(14): error C2065: 'inFile': undeclared identifier
1>c:\users\bogdan\documents\c & c++ programs\writing and reading a txt file\writing and reading a txt file\writing and reading a txt file.cpp(15): error C2065: 'cout': undeclared identifier"
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
int main()
{
ifstream inFile;
inFile.open("C:\Users\Bogdan\Documents\UID.txt");
int x;
inFile >> x;
cout << x;
return 0;
}
Upvotes: 0
Views: 240
Reputation: 283
This is a small example in C++ show you how to open a file using fstream, after i add a loop while ( getline (myfile,line) ) that check if the filestream has a line, if it has a line then the program will print it cout << line << '\n'; if not the program will exit.
#include <iostream>
#include <fstream>
#include <string>
int main () {
string line;
std::ifstream myfile ("/path/to/file.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::cout << line << std::endl;
}
myfile.close();
}
else std::cout << "Error unable to open file" << std::endl;
return 0;
}
Upvotes: 0
Reputation: 463
ifstream
and cout
are both part of the std
namespace. You aren't using namespace std
, so you need to include the namespace whenever you reference them. A fixed version of your code would be:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream inFile;
inFile.open("C:\Users\Bogdan\Documents\UID.txt");
int x;
inFile >> x;
std::cout << x;
return 0;
}
Upvotes: 1