Hi
if you have a file on c drive under a particular folder how can you open it

ifstream input(filename);

if your file name is available in c drive on myfile folder (filename=" C:\myfile\hi.txt") :idea:

Dani AI

Generated

A few practical points that expand on 's question and 's note about escaping.

In C/C++ string literals the backslash introduces escape sequences, so a Windows path must be represented correctly in the program. If using an older compiler, escape each backslash or use an alternative form. With C++11+ you can avoid repeated escapes by using a raw string literal. Example:

#include <fstream>

std::ifstream f;
f.open(R"(C:\myfile\hi.txt)");
if (!f.is_open()) {
    // handle error
}

For modern, portable code prefer std::filesystem (C++17). It makes joining parts, checking existence, and handling OS-specific quirks easier:

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

std::filesystem::path p = "C:/myfile/hi.txt";
if (!std::filesystem::exists(p)) {
    std::cerr << "Not found: " << p << '\n';
} else {
    std::ifstream f(p);
    // ...
}

Troubleshooting tips: ensure the string has no accidental leading/trailing spaces (the original post shows a space before the path), use an absolute path if unsure about the process working directory, check permissions, and report the system error when open fails (for example via std::strerror(errno)). Raw string literals and std::filesystem require C++11 and C++17 compilers respectively — if those are unavailable stick to escaped backslashes or forward-slash versions of the path. For more on std::filesystem see the reference at std::filesystem.

Recommended Answers

All 2 Replies

\\ in literal strings = \ because a single \ "escapes" the next character

"C:\\myfile\\hi.txt"

Thank you

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.