简体   繁体   English

如何从 c++ 中的文件读入 [暂停]

[英]How do I read in from a file in c++ [on hold]

I am having trouble reading in characters from a file.我无法从文件中读取字符。 When this code runs it says it cannot read in from the file.当此代码运行时,它说它无法从文件中读取。 The text file is in the same folder as main.cpp (not sure if this is a problem).该文本文件与 main.cpp 位于同一文件夹中(不确定这是否有问题)。

char read_from_file;

std::cout << "You picked a pre-written file to play against" << std::endl;
std::cout << "A pre determined choice has been picked, what do you choose?" << std::endl;

std::ifstream inFile;
inFile.open("rps.txt");
inFile >> read_from_file;

if (inFile.is_open())
{
   std::cout << "Successful" << std::endl;
}
else
{
   std::cout << "Unable to open file" << std::endl;
}
inFile.close();

Try full path to "rps.txt".尝试“rps.txt”的完整路径。 It should work.它应该工作。

It sounds like you've got this already, but just for completeness, the file you want to read from should be in the same directory as your executable.听起来您已经有了这个,但为了完整起见,您要读取的文件应该与可执行文件位于同一目录中。

dir
  |- main.cpp
  |- rps.txt
  |- a.out*

* or whatever the name of your program is

I just put a single 'R' in my rps.txt file.我只是在我的 rps.txt 文件中放了一个“R”。 Your code is mostly good.你的代码大多是好的。 There is one key difference, and that is that you need to check that you opened the file immediately after you attempt to open it.有一个关键区别,那就是您需要在尝试打开文件后立即检查您是否打开了该文件。 You need to know if it's open before you try to read from it.在尝试读取之前,您需要知道它是否已打开。

#include <iostream>
#include <fstream>

int main()
{
    char read_from_file;

    std::cout << "You picked a pre-written file to play against" << std::endl;
    std::cout << "A pre determined choice has been picked, what do you choose?" << std::endl;

    std::ifstream inFile;
    inFile.open("rps.txt");
    if (!inFile.is_open())  // You would probably flesh this out with an error
        return 1;           // message or something

    inFile >> read_from_file;

    if (inFile.is_open())
        std::cout << "Successful" << std::endl;
    else
        std::cout << "Unable to open file" << std::endl;
    inFile.close();
}

This worked just fine for me.这对我来说很好。 You are not specific on what your error message is.您没有具体说明您的错误消息是什么。 Are you getting the "Unable to open file" from your code, or are you getting a system message?您是从代码中收到“无法打开文件”,还是收到系统消息? The solution is likely to ensure that the file can be read from, and maybe also executed.该解决方案很可能确保文件可以被读取,也可以被执行。 For safety, you can run chmod 777 rps.txt * from a terminal to give full permissions to the file.为了安全起见,您可以从终端运行chmod 777 rps.txt * 以授予对文件的完全权限。

I'm also going to comment that because all of my if and else statements are just single statements, I am safe to leave out the { } .我还要评论说,因为我所有的ifelse语句都只是单个语句,所以我可以安全地省略{ } If you need a block to more than one thing, bring back the curly braces, and if one branch of a larger block requires curly braces, then all branches should get them for consistency/readability.如果您需要一个块来做不止一件事,请带回花括号,如果较大块的一个分支需要花括号,那么所有分支都应该获取它们以保持一致性/可读性。

* Linux and macOS. * Linux 和 macOS。 You can right click a file in Windows and set permissions there.您可以右键单击 Windows 中的文件并在那里设置权限。 Probably PowerShell too, but I don't have the foggiest how to go about that.也可能是 PowerShell,但我没有关于 go 的最模糊的方法。

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

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