从文件中读取,只读取文本,直到它变成空白
Reading from a file, only reads text untill it gets to empty space
我成功地读取了文件中的文本,但它只读取,直到它遇到一个空白的空间,例如文本:"嗨,这是一个测试",cout's as: "嗨,".
去掉","没有影响。
我想我需要添加类似于"inFil.ignore(1000,'n');
"的东西到下面的代码位:
inFil>>text;
inFil.ignore(1000,'n');
cout<<"The file cointains the following: "<<text<<endl;
我不希望更改为getline(inFil, variabel);
,因为这将迫使我重做一个本质上工作的程序。
谢谢你的任何帮助,这似乎是一个非常小的和容易修复的问题,但我似乎找不到一个解决方案。
std::ifstream file("file.txt");
if(!file) throw std::exception("Could not open file.txt for reading!");
std::string line;
//read until the first n is found, essentially reading line by line unti file ends
while(std::getline(file, line))
{
//do something line by line
std::cout << "Line : " << line << "n";
}
这将帮助您读取文件。我不知道你想要实现什么,因为你的代码不完整,但上面的代码通常用于在c++中读取文件。
您一直在使用格式化提取来提取单个字符串,一次:这意味着单个单词。
如果你想要一个包含整个文件内容的字符串:
std::fstream fs("/path/to/file");
std::string all_of_the_file(
(std::istreambuf_iterator<char>(filestream)),
std::istreambuf_iterator<char>()
);
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- C++忽略使用字符串流进行读取的文本文件中的空白
- 从文本中抓取句子,将所有句子分别存储在某个数据结构中
- 从二进制文件中抓取文本时,为什么 xdg_vtnr=8 是我的结果
- C++ 中的列文本抓取
- Windows XP在C++中启动时读取文本文件的速度较慢;预取
- 从Windows应用程序的文本框中抓取文本
- 使用UDP和套接字进行客户端-服务器文本文件传输 在C++中,服务器创建一个空白文件
- 从文件中读取,只读取文本,直到它变成空白
- 从文本文件中读取空白
- 将文本文件的内容一个字符一个字符地读入矢量,不跳过空白或新行
- 如何在c++中使用字符串getline从文本文件中读取时忽略/跳过空白
- 从文本中读取行,删除空白并保存到新文件