在 c++ 中读取格式化的文本文件
Formatted text file reading in c++
这是文件。
R: 10
Steve Abrew 90 80 84 76
David Nag 93 87 90 80
Mike Black
Andrew Van Den 90 88 95 85
Chris Smith 86 74 90
Dennis Dudley 74 76 77 83
Leo Rice 95 75
Fred Flinstone 73 67 78 72
Dave Light 89 71 91 89
Hua Tran Du 81 79 80
我想从文件中读取这些内容,并分别为字符创建一个变量,为整数创建一个变量。但做不到..
这是我的代码:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc,char* argv[])
{
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr<<"Couldn't find the file";
return -1;
}
string data;
getline(inFile,data,' ');
cout<<data<<"n";
return 0;
}
输出现在与文件相同。但是一切都保存在字符串数据中。但是现在我如何将名称和 int 与该字符串分开?
非常感谢,如果你帮忙:)
好的,我得到了格式。
我可以告诉你怎么做。在此之前,请创建一个名为 N
的 int 变量,以及一个deque<string>
型容器Characters
和deque<deque<int>>
型容器Data
。(当然要做#include <deque>
)(还有#include <sstream>
和#include <string>
)
首先,输入"R:"部分 -> inFile >> temp;
其次,输入将收集多少行数据> inFile >> N;
。
第三,输入数据。
for(int i = 0;i<N;i++)
{
string Fn, Ln;
inFile >> Fn >> Ln;
Characters.push_back(Fn+" "+Ln); //name
string A;
getline(inFile,A);
stringstream ss(A);
int W;
deque<int> TempData;
while(ss>>W)
{
TempData.push_back(W);
}
Data.push_back(TempData);
}
现在,名称位于Characters
容器中,其数据位于Data
容器中!
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入