从外部文件获取输入?
我需要从 C++ 的外部文件中获取非常基本的输入。我尝试在互联网上搜索几次,但没有任何内容真正适合我的需要。这将是一个 .txt 文件,其输入来自其中,并且将填充如下行:
131
241
371
481
我已经有代码可以手动获取此输入,它看起来像这样:
using namespace std;
//Gets the initial values from the user.
int control=0;
while (rowb!=0){
cout << "Row: ";
cin >> rowb;
cout << "Column: ";
cin >> columnb;
cout << "Number: ";
cin >> numb;
row[control]=rowb-1;
column[control]=columnb-1;
num[control]=numb;
control++;
}
这是解决 sudoko 板问题的程序的一部分。输入的数字是数独板所保存的初始值,用户正在输入来自板的行、列和数字。
我需要的是能够创建一个 .txt 文件,其中这些数字存储在行中,这样我就不必输入这么多数字。我几乎不知道如何去做这件事。主要是,当我向其中添加更多代码时,我只会使用 txt 文件来测试我的程序。我的程序中需要输入 150 多个数字才能得到一块板,而且需要很多时间。任何意外输入的错误值也是一个大问题,因为我必须重新开始。那么我如何让 C++ 读取文本文件并使用这些数字作为输入呢?
I need to get very basic input from an external file in C++. I tried searching the internet a few times but nothing really applied to what I need. This would be a .txt file that the input it coming from, and it would be filled with lines like this:
131
241
371
481
I have code already to manually get this input, and it looks like this:
using namespace std;
//Gets the initial values from the user.
int control=0;
while (rowb!=0){
cout << "Row: ";
cin >> rowb;
cout << "Column: ";
cin >> columnb;
cout << "Number: ";
cin >> numb;
row[control]=rowb-1;
column[control]=columnb-1;
num[control]=numb;
control++;
}
This is part of a program that solves sudoko boards. The inputed numbers are the initial values that a sudoko board holds, and the user is inputing the row, column, and number that comes from a board.
What I need is to be able to create a .txt file with these numbers stored in rows so that I do not have to enter so many numbers. I have very little idea how to go about doing this. Mainly I'll only be using the txt file for testing my program as I move along with adding more code to it. It takes 150+ entered numbers within my program just to get a single board, and it takes a lot of time. Any accidentally wrong entered value is also a huge problem as I have to start again. So how would I get C++ to read a text file and use those numbers as input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了其他建议之外,您可以简单地将文件重定向到标准输入,如下所示(其中
$
是命令提示符):这将像平常一样运行
myprogram
但需要从mytextfile.txt
输入,就好像您已经输入一样。根本不需要调整您自己的程序。(这适用于 Unix/Linux 系统和 Windows。)
Aside from the other suggestions, you can simply redirect a file to standard input, like so (where
$
is the command prompt):That will run
myprogram
just as normal but take input frommytextfile.txt
as if you had typed it in. No need to adjust your own program at all.(This works on both Unix/Linux systems and on Windows.)
您可以使用标题
中的std::ifstream
打开文件进行输入,然后像从std::cin< 一样读取该文件。 /代码>。
显然,您可以在循环中使用
>>
来读取多个数字。You can open a file for input with
std::ifstream
from the header<fstream>
, then read from it as you would fromstd::cin
.Obviously, you can use
>>
in a loop to read multiple numbers.创建一个
std::ifstream
对象,然后像从std::cin
中一样读取它。至少如果我理解你想要做什么,作为第一个输入的131
实际上是三个单独的数字(1
,3 和 <代码>1)。如果是这样,最简单的方法可能是稍微更改输入文件以在每个文件之间添加一个空格:
Create an
std::ifstream
object, and read from it just like you would fromstd::cin
. At least if I understand what you're trying to do, the131
as the first input is really intended to be three separate numbers (1
,3
, and1
). If so, it's probably easiest to change your input file a bit to put a space between each:就我个人而言,我会从不同格式的文件开始:为每个单元格输入一个值。也就是说,输入文件中的每一行都代表数独板上的一行。空字段将使用空格字符。直接的优点是输入实际上看起来很像数独板。另外,您最多可以输入 90 个字符: 棋盘 9 个字符,每行一个换行符:
这将采用如下输入:
请注意,每一行都使用 9 个字符。您可能想要使用更明显的内容,例如
.
。Personally, I would start with a different format of the file: enter a value for each cell. That is, each row in the input file would represent a row in the sudoko board. Empty fields would use a space character. The immediate advantage is that the input actually pretty much looks like the sudoko board. Also, you would enter at most 90 characters: 9 characters for the board and a newline for each line:
This would take input like this:
Note that each of these lines uses 9 characters. You might want to use something more visible like
.
.