By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

How to open text file using c++

P: 1
I have written a program that can open and read a text document. I compiled the code with no errors but I am still not able to open the text document. I have checked that the location where the text file is kept has full access.The text file resides in inFile.open("C:\\Users\LJ\\Desktop\\CPPExamples\\S ection4\\Delta_T\\Test01.txt"); where Delta_T is a folder. Please view my code.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream inFile;
inFile.open("C:\\Users\LJ\\Desktop\\CPPExamples\\S ection4\\Delta_T\\Test01.txt");

//Check for error

if (inFile.fail())
{
cout << "Error Opening File";
}
else{
string item;
// while(inFile >> item)
while(getline(inFile, item))
{
cout << item << endl;
}
inFile.close();
}
system("pause");
}
Apr 2 '19 #1
Share this Question
Share on Google+
1 Reply


dev7060
100+
P: 115
I ran the code in Code::Blocks, everything worked fine for me. What IDE are you using?
Apr 2 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.