How to open text file using c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LyricleG
    New Member
    • Apr 2019
    • 1

    How to open text file using c++

    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\\Des ktop\\CPPExampl es\\Section4\\D elta_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\\Des ktop\\CPPExampl es\\Section4\\D elta_T\\Test01. txt");

    //Check for error

    if (inFile.fail())
    {
    cout << "Error Opening File";
    }
    else{
    string item;
    // while(inFile >> item)
    while(getline(i nFile, item))
    {
    cout << item << endl;
    }
    inFile.close();
    }
    system("pause") ;
    }
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    I ran the code in Code::Blocks, everything worked fine for me. What IDE are you using?

    Comment

    Working...