从文本文件中读取和存储数据
Reading and Storing data from text file C++
我有一个类似于这里发布的问题:
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;
}
相关文章:
- 使用 char* 存储数据和产生的错误
- 我可以存储数据的地方/C++,Linux,QtCreator
- 如何在共享库的整个生命周期内存储数据
- 在字符串数组中动态创建和存储数据
- 用指向文件的指针存储数据结构
- 如何存储数据/我们如何获取存储在 unordered_map c++11 中存储桶中的数据
- 存储数据的对象和存储指向数据的智能指针的对象
- C++读取文本文件并存储数据
- Cocos2dx: 无法存储数据?
- 如何在每次循环迭代期间生成向量,存储数据,然后删除该向量?
- 尝试从每个预期数据之间有多个空间的文件中读取和存储数据
- 存储数据列表并自动分配给结构的最佳方法是什么
- QT MAC应用程序存储数据路径
- 动态分配存储数据在堆中的随机位置中
- 鸟类调查中的项目在代码中遇到问题.如何像数据库系统一样存储数据
- 可以在C 中永久存储数据中的数据
- 用于存储数据的结构和外部二进制文件
- 使用 char 数组存储数据
- 在结构中读取和存储数据
- 尝试在结构中存储数据时出现堆分配错误