简体   繁体   English

从外部文件获取输入?

[英]Getting input from external files?

I need to get very basic input from an external file in C++. 我需要从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: 这将是一个.txt文件,它是输入的来源,并且将用以下行填充:

131 131
241 241
371 371
481 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. 这是解决sudoko板程序的一部分。 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. 输入的数字是sudoko板保存的初始值,并且用户正在输入来自板的行,列和编号。
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. 我需要的是能够创建一个.txt文件,并将这些数字存储在行中,这样我就不必输入太多数字了。 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. 主要是,随着我向其中添加更多代码,我只会使用txt文件来测试程序。 It takes 150+ entered numbers within my program just to get a single board, and it takes a lot of time. 我的程序中需要150多个输入的数字才能获得一块板,并且花费大量时间。 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? 因此,我如何让C ++读取文本文件并将这些数字用作输入?

Aside from the other suggestions, you can simply redirect a file to standard input, like so (where $ is the command prompt): 除了其他建议外,您还可以将文件重定向到标准输入,如下所示(其中$是命令提示符):

$ myprogram < mytextfile.txt

That will run myprogram just as normal but take input from mytextfile.txt as if you had typed it in. No need to adjust your own program at all. 这将像通常一样运行myprogram ,但是就像输入它一样从mytextfile.txt接受输入。根本不需要调整自己的程序。

(This works on both Unix/Linux systems and on Windows.) (这适用于Unix / Linux系统和Windows。)

You can open a file for input with std::ifstream from the header <fstream> , then read from it as you would from std::cin . 您可以从头文件<fstream>使用std::ifstream打开一个文件以供输入,然后像从std::cin一样从中读取文件。

int main()
{
    std::ifstream input("somefile.txt");
    int a;
    input >> a;  // reads a number from somefile.txt
}

Obviously, you can use >> in a loop to read multiple numbers. 显然,您可以在循环中使用>>来读取多个数字。

Create an std::ifstream object, and read from it just like you would from std::cin . 创建一个std::ifstream对象,并像从std::cin一样读取它。 At least if I understand what you're trying to do, the 131 as the first input is really intended to be three separate numbers ( 1 , 3 , and 1 ). 至少如果我理解你想要做什么,将131作为第一个输入是真正意图是三个独立的数字( 13 ,和1 )。 If so, it's probably easiest to change your input file a bit to put a space between each: 如果是这样,更改一下输入文件以在每个文件之间留一个空格可能是最简单的:

1 3 1
2 4 1
3 7 1
4 8 1

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. 也就是说,输入文件中的每一行将代表sudoko板中的一行。 Empty fields would use a space character. 空字段将使用空格字符。 The immediate advantage is that the input actually pretty much looks like the sudoko board. 直接的好处是输入实际上实际上看起来像sudoko板。 Also, you would enter at most 90 characters: 9 characters for the board and a newline for each line: 另外,您最多可以输入90个字符:白板9个字符,每行换行:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

int main(int ac, char* av[])
{
    std::ifstream in(ac == 1? "sudoko.init": av[1]);
    char board[9][9];
    for (int i(0); i != 9; ++i)
    {
        in.read(board[i], 9).ignore();
    }
    if (!in)
    {
        std::cout << "failed to read the initial board\n";
    }
    else
    {
        typedef std::ostream_iterator<char> iterator;
        std::fill_n(iterator(std::cout << "board:\n\n+", "+"), 9, '=');
        for (int i(0); i != 9; ++i)
        {
            std::copy(board[i] + 0, board[i] + 9, iterator(std::cout << "\n|", "|"));
            std::fill_n(iterator(std::cout << "\n+", "+"), 9, (i + 1) % 3? '-': '=');
        }
        std::cout << "\n";
    }
}

This would take input like this: 这将需要这样的输入:

 4  5 3 8
71   3   
   16  7 
   6 4  7
  6   8  
1  9 5   
 6  42   
   5   94
4 7 9  3 

Note that each of these lines uses 9 characters. 请注意,每行使用9个字符。 You might want to use something more visible like . 您可能想要使用更可见的内容. .

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

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