Getting input from external files?

我的未来我决定 提交于 2019-12-02 17:28:35

问题


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?


回答1:


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.




回答2:


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




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/8773686/getting-input-from-external-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!