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.
    inputFile.open(filename);

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

asked Apr 12 in C++ by rugan
, 351 views Last updated : Sep 22, 2017

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 3 days ago 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...

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