I wanna have my program be able to let the user manually open a text file by destination and retrieve data from it, I have everything like ifstream good, i just need to know how to let the user cin something like C:/files/myfile.txt, but with the ifstream format I need to use the format ifstream stringname("texthere"); but cant let the user modify the "texthere", im sure its just a simply command, plz help

Dani AI

Generated

Short summary: pointed in the right direction — reading a full line for the path and opening an input stream is the right idea. Common reasons it still "doesn't work" in Dev-C++ are missing the input/header needed for console reading, not checking whether the file actually opened, path-format issues (quotes, backslashes, spaces), or using a different working directory than expected. The checklist below covers practical checks and a modern alternative.

  • Ensure the program actually reads the entire path string (console input functions require the right header and the right call so spaces are preserved) and that symbols are correctly qualified or a using-declaration is present. Confirm the captured string exactly matches the path expected (no surrounding quotes or stray whitespace).
  • If the path was entered as a literal in source code, backslashes must be escaped; when typed at the console this is not necessary, but surrounding quotes will become part of the string and cause open to fail.
  • After attempting to open the file, test the stream state instead of assuming success. Use the stream's open/state methods to detect failure and print the captured path and the error condition for diagnosis (basic_ifstream::is_open).
  • If a relative path is used, Dev-C++/MinGW can run the process with a different working directory than the source or project folder. Try running the compiled .exe from Explorer (or print the working directory) to verify where the program is looking.
  • Check OS-level issues (file actually exists, permissions, locked by another process).

For a modern check-before-open approach, C++17 provides filesystem utilities to test existence before opening; see std::filesystem::exists. Tying this back to 's suggestion: capture the entire path, verify the string, then explicitly check the stream state after opening to find the real cause.

Recommended Answers

All 3 Replies

string dir_name;
	getline(cin, dir_name);
	ifstream file(dir_name.c_str());

still doesnt work, any specific header for this, just dont work on dev c++

#include <string>
#include <fstream>
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.