Hi,

I've been trying to read the file content from the file path entered by user at run time. However i always end up in error.

Help needed...

char name[100];
ifstream infile;
cout<<"Enter filename to open";
cin.getline(name,100);
infile.open(name);
if(!(infile.is_open()))
{
    cout<<endl<<"File Not Found";

}

i'm not using relative path, instead the full path

You quick reply will be appreciated

thanks

Dani AI

Generated

A short troubleshooting guide that expands on what started — an inability to open a file can have many causes other than "file does not exist." The quickest way to diagnose is to (1) print exactly what the program is trying to open, (2) check existence separately from open failures, and (3) capture a system error when open fails so you know whether it is permissions, locking, length or encoding.

A compact, modern pattern (C++17+) that shows these checks:

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

namespace fs = std::filesystem;

int main() {
    std::string path;
    std::cout << "Enter filename: ";
    std::getline(std::cin, path);
    std::cout << "Trying: '" << path << "'\n";

    if (!fs::exists(path)) {
        std::cerr << "Path not found\n";
        return 1;
    }

    std::ifstream in(path, std::ios::binary);
    if (!in) {
        std::cerr << "File exists but cannot be opened (permissions, busy, wrong mode)\n";
        return 2;
    }

    // proceed to read from 'in'
}

If you cannot use <filesystem>, use a simple C fallback to get an OS error code (useful for diagnostics):

FILE *f = std::fopen(path.c_str(), "rb");
if (!f) std::cerr << "fopen failed: " << std::strerror(errno) << '\n';
else std::fclose(f);

Final practical notes: make sure you read the full line (paths often contain spaces), strip surrounding quotes or trailing whitespace, run the program from the same working directory you expect, and check permissions or locks. On Windows watch for MAX_PATH and non-ASCII names (use wide APIs or the \?\ prefix for very long paths). These checks will quickly tell whether the problem is a mistyped path, a permissions/lock issue, or an environment/encoding limitation.

Recommended Answers

All 2 Replies

I don't see any mistake in your code, it works fine with me.
Ensure that the file you're trying to open exists.

But...
Technically this line displays incorrect information: cout<<endl<<"File Not Found"; It's not because the file couldn't be opened, that the file automatically doesn't exist.
For example it could be that you try to open an existing file, but this fails because of certain circumstances, then the file exists, and you display "File not found" .

thx, i've got sorted it out.

I don't see any mistake in your code, it works fine with me.
Ensure that the file you're trying to open exists.

But...
Technically this line displays incorrect information: cout<<endl<<"File Not Found"; It's not because the file couldn't be opened, that the file automatically doesn't exist.
For example it could be that you try to open an existing file, but this fails because of certain circumstances, then the file exists, and you display "File not found" .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.