简体   繁体   English

我如何用 ifstream 解决这个问题

[英]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"当我运行此代码时,它仅适用于 Visual Studio,在线编译器中总是告诉我“无法打开文件”

 #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.这意味着当您尝试打开"C:/Users/ACER/Desktop/words.txt"时,实际上是在尝试打开位于其服务器上的文本文件。 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.我经常使用的一个确实有能力这样做,以防您正在寻找一个: https://replit.com/@Ranoiaetep/IndianredSquigglyPrinters 。请注意,工作目录默认为您的根目录。

An online compiler probably cannot open file on your computer using C++ code since that's not how the web works.在线编译器可能无法使用 C++ 代码在您的计算机上打开文件,因为这不是 web 的工作方式。 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.它必须通过一些 Javascript 系统进行路由,这是可以创建的,但是创建者在设置编译器时似乎没有实现。

You might try using file://C:/Users/ACER/Desktop/words.txt and double checking your path.您可以尝试使用file://C:/Users/ACER/Desktop/words.txt并仔细检查您的路径。 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. Web 浏览器在访问本地文件时使用该file:// ,即如果您的计算机上有一个 web 页面,它可能类似于file://C:/Users/ACER/Documents/index.html

If that doesn't work, it just doesn't have the capability to load or save local files like that.如果这不起作用,它只是没有能力加载或保存这样的本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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