从外部文件获取输入?

发布于 2024-12-25 11:52:03 字数 850 浏览 3 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2025-01-01 11:52:03

除了其他建议之外,您可以简单地将文件重定向到标准输入,如下所示(其中 $ 是命令提示符):

$ myprogram < mytextfile.txt

这将像平常一样运行 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):

$ 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.

(This works on both Unix/Linux systems and on Windows.)

情话难免假 2025-01-01 11:52:03

您可以使用标题 中的 std::ifstream 打开文件进行输入,然后像从 std::cin< 一样读取该文件。 /代码>。

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

显然,您可以在循环中使用 >> 来读取多个数字。

You can open a file for input with std::ifstream from the header <fstream>, then read from it as you would from 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.

也只是曾经 2025-01-01 11:52:03

创建一个 std::ifstream 对象,然后像从 std::cin 中一样读取它。至少如果我理解你想要做什么,作为第一个输入的 131 实际上是三个单独的数字(1, 3 和 <代码>1)。如果是这样,最简单的方法可能是稍微更改输入文件以在每个文件之间添加一个空格:

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

Create an std::ifstream object, and read from it just like you would from 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). 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
我的痛♀有谁懂 2025-01-01 11:52:03

就我个人而言,我会从不同格式的文件开始:为每个单元格输入一个值。也就是说,输入文件中的每一行都代表数独板上的一行。空字段将使用空格字符。直接的优点是输入实际上看起来很像数独板。另外,您最多可以输入 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";
    }
}

这将采用如下输入:

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

请注意,每一行都使用 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:

#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. You might want to use something more visible like ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文