从文本文件中读取和存储数据

Reading and Storing data from text file C++

本文关键字:存储 数据 读取 文本 文件      更新时间:2023-10-16

我有一个类似于这里发布的问题:

c++:从文本文件中读取并分离成变量

不同之处在于原始帖子的file.txt格式如下:

  • name int int
  • name int int

我的文件格式如下:

  • 帧lname

  • int int

  • 帧lname

  • int int

我试图在(第一个和最后一个)中读取名称并将其存储到单个变量和每个int到自己的单独变量中(用于将来的计算)。

我已经尝试使用类似的答案,但是换行弄乱了我的变量。

作为额外的奖励,我的文件中有随机的空行。

这是我到目前为止写的代码

if (myfile.is_open())
{    
    while (getline(myfile, line))
    {
        istringstream iss(line);
        string fname;
        string lname;
        if (line != "")
        {
            iss >> fname >> lname >> x >> y >> z >> a >> b >> c;
            cout << "name: " << fname << endl;
            //iss >> x >> y >> z >> a >> b >> c;
            cout << "x: " << x << "y: " << y << endl;
        }
        else cout << "";
    }
}

你需要两行,并打破你的变量阅读。

iss >> fname >> lname >> x >> y >> z >> a >> b >> c; //don't know why you have 6 ints when you specified 3, I'll assume 6 is correct

issFirstLine >> fname >> lname;
issSecondLine >> x >> y >> z >> a >> b >> c;

现在有多种方法可以做到这一点。两个选项是:你可以尝试分别读取两行,只有当你有两行非空时才进行打印,或者你可以使用某种变量来确定你是在一个名称行还是一个int行上,相应地打印,并在成功输出的末尾更改预期的类型。

您可以使用freopen()读取文件。这是没有问题的,如果多个空白行之间的愿望输入行。

freopen("test.txt","r",stdin);
string fname;
string lname;
int x, y,z ,a ,b ,c;
while(cin>>fname){
cin>>lname>> x >> y >> z ;
//your other code
}

离得太近了。

我建议通过引入一个函数来帮助消除空行来简化:

istream & getNonBlankLine(istream & in,
                           string & line)
{
    while (getline(in, line) && line.length() == 0)
    {
        //does nothing. If the loop didn't exit, line was blank
        // if the loop exited, either we're out of lines or we got a valid line
        // out of lines case is handled by testing the stream state on return 
    }
    return in; // lets us chain and also lets us easily test the state of the stream
}

然后回到手头的工作:

if (myfile.is_open())
{
    string nameline;
    string numberline;
    while (getNonBlankLine(myfile, nameline) &&
           getNonBlankLine(myfile, numberline))
    { // got two non-blank lines and the stream is still good
        //turn lines into streams
        stringstream namess(nameline);
        stringstream numberss(numberline);
        // parse streams 
        if (namess >> fname >> lname && // read in first and last name or return error 
            numberss >> x >> y >> z) // read in x, y, and z or return error
        {
            // do stuff with input
        }
        else
        { //something did not read in correctly. Bad line
            cerr << "invalid input"<< endl; // notify user and abort
            return -1;
        }
    }
}

一个未经请求的更改是检查所有输入的有效性。如果输入不符合,OP的cut和所有的答案到目前为止都会无声地失败。第一行必须有两个名字,第二行必须有三个整数。其他任何情况都应该触发无效的输入错误消息和过早退出。我说应该是因为我还没有测试过。

这样应该可以。

#include <stdio.h>
int main(int argc, char** argv) {
    FILE *fin = fopen("in.txt", "r");
    char fname[1024], lname[1024];
    int x, y, z;
    while (fscanf(fin, "%s %s %d %d %d", fname, lname, &x, &y, &z) == 5) {
        printf("%s %s %d %d %dn", fname, lname, x, y, z);
    }
    return 0;
}