0

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; } 
8
  • 1
    please let is know what the error code / text is. Commented Feb 7, 2019 at 18:41
  • 1
    And you need to escape your \ so replace \ with \\ Commented Feb 7, 2019 at 18:42
  • Sorry, my bad, i forgot to tell that. The 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: Commented Feb 7, 2019 at 18:42
  • 2
    When asking about build errors, always include the actual errors, in full and complete, including any possible informational notes. And copy-pasted as text. So please edit your question to include that. Commented Feb 7, 2019 at 18:42
  • @Someprogrammerdude I rewrote my question Commented Feb 7, 2019 at 18:48

2 Answers 2

1

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; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Brandon! I added this line: using namespace std; (because i am not used with std::) and now it works...partially; In the txt file i have the number 123 but in the output it is something like -880513. I ll fix this one myself :D Thanks for the help
That looks like an uninitialized int. I'd bet the file is failing to open, which usually means the file path is wrong.
You were right. I modifyed the file path and now it works perfectly!
0

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; } 

2 Comments

What have you changed? Why? Code-only answers encourages cargo-cult programming which is bad. Not to mention your program does a lot that the OP doesn't really ask about.
version without using using namespace std; to avoid pollutes the global namespace and increase compile time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.