Error opening a .txt file in C++

0 votes

I have been trying to read a .txt file to read the file and store it in different variables. But, the program keeps saying that it could not open the file. I placed the .txt file in the folder where the .exe file is. Below is my code.

Thanks for any help!

int number, numIntegers = 0;
    vector<int> vect;

    //Get the filename from the user.
    cout << "Please enter the filename: ";
    getline(cin, filename);

    //Open the file.;

    if (inputFile) {
        //Read the numbers from the file
        while (inputFile >> number) {
            cout << number << endl;
        } //end while
        //Close the File
    }//end if
    else {
        cout << "Error opening the file." << endl;
    } // end else

asked Apr 12, 2017 in C++ by rugan
, 543 views Last updated : Feb 20, 2018

1 Answer

0 votes
do it in following way
var fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
    string line;
    while ((line = streamReader.ReadLine()) != null)
        // process the line
answered Sep 18, 2017 by LogenRahul

Related questions

what is opening file in c

fopenTo open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on...

c 2 Answer 67 views asked May 20, 2017 in Technology by Govind123