C++文本文件的获取线
C++ getline for text file
我希望真的很简单。我已经打开了一个文本文件,我希望它使用 getline 读取第一行。我希望它采用该行中的所有整数,用逗号分隔,并将它们加在一起以存储在变量中。但是,我不太确定如何使用分隔符进行设置。
#include <fstream>
#include <iostream>
int main(){
std::fstream codeBreaker;
int x,y,z;
codeBreaker.open("input.txt");
void decipher();
void encode();
std::cout << "Have a nice day!" << std::endl;
return 0;
}
void decipher(){
}
void encode(){
}
您已经概述了第一步:在第一行中阅读std::getline
.你错过的一点是,几乎每当你读取数据时,你都想检查读取是否成功。
std::string line;
if (std::getline(infile, line))
// we got some data
else
// the read failed
从那里,您需要将该行解析为单个数字。一种方法是stringstream
.
std::stringstream buffer(line);
由于您的数字用逗号分隔,因此您需要读取(但忽略(逗号,然后读取下一个数字。继续直到到达行尾,您通常会通过检查读取数字是否成功来发现。
int number;
while (buffer >> number) {
char ch;
buffer.get(ch);
if (ch != ',')
// unexpected input ?
// do something with the number we just read here
}
相关文章:
- 获取文件数据预处理器宏
- 如何在Windows内核中获取文件大小
- 从 C 中的变量获取文件的路径,C++
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- 如何使用 jpeglib.h 获取文件是否为 JPEG 类型
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- 我需要通过窗口句柄(HWND)获取文件,我该怎么办?
- 如何用C++部分地获取文件中的行
- 使用内置的Windows方法获取文件夹的内容
- 在 Apache Velocity 模板语言中获取文件目录
- 如何在C#或C++中获取文件或文件夹的有效权限?有什么 API 吗?
- 对字符求和并减去以获取文件
- 从 FutureAccessList 同步获取文件夹路径
- 通过HTTPS从POCO StreamCopier获取文件下载进度
- 如何使用C 17获取文件大小
- getline 无法获取文件的第一行
- 如何使用 Win API 获取文件夹中的文件列表(带过滤器)
- 无法从零MQ ZMQ_SERVER套接字中获取文件描述符
- HttpQueryInfo 获取文件大小
- 通过文件资源管理器获取文件夹目录