简体   繁体   中英

how i could solve this problem with ifstream

when I run this code its only work with visual studio, in online compiler always tell me "cant opening file"

 #include <iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>
    using namespace std;
    ifstream input("C:/Users/ACER/Desktop/words.txt");
    if (!input.is_open())
    {
        cout << "cant opening file";
        return 0;
    }

When you were attempting to use the code on an online compiler, most of the time you are actually compiling the code on their server, instead of on your computer.

What that means is that when you are trying to open "C:/Users/ACER/Desktop/words.txt" , you are actually attempting to open a text file that is located on their server. And in most cases, you wouldn't be permitted to lookup any directory other than the one you are in. Even if you were permitted to do that for some reason, the chances they also have that text file on their server in the same location is gonna be really low.


Some comments above mentioned that many online compilers often don't have the facility to manipulate files. One that I often use do have the ability to do so in case you were looking for one: https://replit.com/@Ranoiaetep/IndianredSquigglyPrinters .Note that the working directory is defaulted as your root directory.

An online compiler probably cannot open file on your computer using C++ code since that's not how the web works. It would have to route through some Javascript system, which is possible to create, however it seems the creators did not implement that when setting up their compiler.

You might try using file://C:/Users/ACER/Desktop/words.txt and double checking your path. Web browsers use that file:// thing when accessing local file ie if you had a web page on your computer it could be like file://C:/Users/ACER/Documents/index.html or something.

If that doesn't work, it just doesn't have the capability to load or save local files like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM