简体   繁体   English

从文件读取数据到单独的Int值

[英]Reading Data from a File into Separate Int Values

I am looking to take in data values from a file and store them in separate integer variables. 我正在寻找从文件中获取数据值,并将其存储在单独的整数变量中。 I have the following variables: 我有以下变量:

int r;
int c;
int startR;
int startC;

The file provides the values in the form: 该文件以以下形式提供值:

2 32 12 4

These are just examples and I need to store these four values in the variables. 这些只是示例,我需要将这四个值存储在变量中。 I am using the getline function to get the line and then calling a split function to take the string and split it and put it into the four values. 我正在使用getline函数获取行,然后调用split函数以获取字符串并将其拆分并放入四个值中。

getline(fileName, line);
split(line);

void split(string l)
{
  //this is where I am struggling to find the best way to get the values stored in the ints
}

Read straight from the file into your variables. 直接从文件中读取变量。

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    std::ifstream file("test.txt");
    int a, b, c, d;
    if (file.is_open()) {
        cout << "Failed to open file" << endl;
        return 1;
    }
    if (file >> a >> b >> c >> d) { // Read succesfull
        cout << a << " " << b << " " << c << " " << d << endl;
    }
    return 0;
}

If you are still having problems parsing the information from the file, you can take a very simple approach -- continuing with your getline beginning to then create a stringstream object from line that will allow you to use the operator making sequential calls to get each next value from your line. 如果仍然无法从文件中解析信息,则可以采用一种非常简单的方法-从getline继续,然后从line创建一个stringstream对象,这将允许您使用运算符进行顺序调用来获取每个下一个您的产品线的价值。 For example: 例如:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main (void) {

    int r, c, startR, startC;           /* variable declarations */
    std::string fname, str;

    std::cout << "Enter a filename: ";  /* prompt for filename */
    std::cin >> fname;

    std::ifstream f (fname);            /* open file */
    if (!f.is_open()) {                 /* validate open for reading */
        std::cerr << "error: file open failed\n";
        return 1;
    }
    std::getline (f, str);              /* read line into str */

    std::stringstream s (str);          /* create stringstream s with str */
    s >> r >> c >> startR >> startC;    /* parse data */

    /* output parsed data for confirmation */
    std::cout << "r: " << r << "  c: " << c << "  startR: " << startR 
            << "  startC: " << startC << "\n";

    f.close();                          /* close file */

    return 0;
}

Example Input File 输入文件示例

$ cat dat/4nums.txt
2 32 12 4

Example Use/Output 使用/输出示例

$ ./bin/strstreamtst
Enter a filename: dat/4nums.txt
r: 2  c: 32  startR: 12  startC: 4

Look things over and let me know if you have further questions. 仔细检查一下,如果您还有其他问题,请告诉我。

You can try something like this. 您可以尝试这样。

FILE* in = NULL;
int r;
int c;
int startR;
int startC;

errno_t fin = freopen_s(&in, "input.txt", "r", stdin);
assert(0x0 == fin);
cin >> r >> c >> startR >> startC;

Here with freopen(), "cin" will read data values from file rather than from standard input. 在这里,使用freopen(),“ cin”将从文件而不是从标准输入中读取数据值。

Your just need to store the value into the appropriate variable. 您只需要将值存储到适当的变量中即可。

See this for more information. 请参阅此以获取更多信息。

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

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