- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有如下所示的良好输入文件:
734 220 915 927 384 349 79 378 593 46 2 581 500 518 556 771 697
571 891 181 537 455
和看起来像这样的错误输入文件:
819 135 915 927 384 349 79 378 593 46 2 581 500 518 556 771 697
551 425 815 978 626 207 931 ABCDEFG 358 16 875 936 899 885 195 565
571 891 181 537 110
两个文件末尾的最后一个整数后面有一个空格。我正在尝试用 C++ 编写一个脚本,它将读取所有整数,除非有第二个示例中的字符/字符串,在这种情况下它会提醒我这一点。我试着这样写:
int main()
{
int n;
bool badfile = false;
ifstream filein("data.txt");
while (!filein.eof())
{
filein >> n;
if(filein.fail())
{
cout << "Not an integer." << endl;
badfile = true;
break;
}
cout << n << " ";
}
cout << endl << "file check: " << badfile << endl;
}
但是 filein.fail()
是由好文件末尾的空格以及坏文件中的字符/字符串触发的。那么我该如何设置它以使其忽略空格呢?为什么它只有在末尾有空格时才会失败,而不是在所有空格处都失败或完全忽略它们?
最佳答案
主要问题是您如何在流上测试 eof()
...它仅在输入尝试尝试读取更多字符时设置文件结尾。首先使用 std::ws
来消耗空格意味着 eof
检测可以是可靠的:如果你不是那么在 eof()
你知道您在一些非空白输入处应该是一个数字 - 如果不是,则您的输入内容有误。
建议代码:
#include <iostream>
#include <fstream>
#include <iomanip>
int main()
{
if (ifstream filein("data.txt"))
{
while (filein >> std::ws && !filein.eof())
{
int n;
if (filein >> n)
cout << n << ' ';
else
{
std::cerr << "error in input\n";
exit(EXIT_FAILURE);
}
}
std::cout << '\n';
}
else
std::cerr << "unable to open data.txt\n";
}
另一种方法出现在下面,它可能更容易理解,但并不完全可靠。问题是,尽管有错误的输入,例如尾随 -
或 +
,您仍然可以到达 EOF,因为在尝试读取数字时会消耗它,但不在本身足以构成对数字的成功解析。仅当已知文件有一个 '\n'
终止最后一行时,这才是可靠的:
int n;
while (filein >> n)
cout << n << " ";
filein.clear(); // remove the error state
if (filein.peek() != istream::traits_type::eof())
{
// while didn't reach EOF; must be parsing error
std::error << "invalid input\n";
exit(EXIT_FAILURE);
}
关于c++ - 与 fileIO 中的空格混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529774/
我想将 csv 文件读入 apache beam 应用程序中的列表,其中列表中的每个元素都是元组或列表(并不重要),这样我就可以得到 csv 1,2,3 4,5,6 成为 [(1,2,3) , (4,
我正在学习 C,并决定从 Java 移植我的 Game of Life 代码。除了FileIO部分之外,看起来并不太难。我的输入文件如下所示: Beehive 5 6 ------ --XX-- -X
我正在尝试创建一个 jar 文件,该文件分析目录中的所有文件以查找提供的字符串并将其替换为提供的字符串 + .getInstance(),我有以下代码: public static String to
ifstream inFile; inFile.open(filename); //open the input file stringstream strStream; strStream << i
我正在尝试制作一个小型图书馆系统,用户可以在其中添加新书的详细信息(名称、作者和价格)。当实现 FileIO 系统以使用 getline 函数从文件中读取每本书的详细信息时,当我尝试将变量存储在临时变
我正在尝试用 c 语言对二进制文件进行简单的写入和读取,但我遇到了困难。这是我尽可能精简的代码: typedef struct car_struct{ char* make; char
在编写程序时,我遇到了一个奇怪的行为 std::ofstream .请引用下面的代码 ofstream dire; dire.open("dir.txt", std::ios::out); // so
在过去的 4 个小时左右,我一直在研究这个,但不知道该怎么做。我正在将我的 Game of Life 移植到 C,但无法让 FileIO 正常工作。输入文件格式如下: Game 1: Pattern
我只是在尝试使用 WinRT,我正在创建的一个演示应用程序是一个基本的“记事本”风格的应用程序,它可以加载/保存到本地存储。虽然我熟悉构建 WinRT 应用程序的正确async 方法,但我的演示应用程
我只是在尝试使用 WinRT,我正在创建的一个演示应用程序是一个基本的“记事本”风格的应用程序,它可以加载/保存到本地存储。虽然我熟悉构建 WinRT 应用程序的正确async 方法,但我的演示应用程
我有如下所示的良好输入文件: 734 220 915 927 384 349 79 378 593 46 2 581 500 518 556 771 697 571 891 181 537 455 和
我正在尝试开发一个基本的 Windows 8 应用商店应用程序,只需创建一个新文件并向其中写入一些示例数据。但是,当我运行我编写的函数时,应用程序挂起并且变得没有响应。我尝试了两种不同的文件创建方法,
我有一个包含通配符的 URL 列表,如何将其传递给 FileIO.match().filepattern(XXXX) 这是代码 PCollectionTuple decompressOut = p
我有一个 KV 的 PCollection,其中键是 gcs file_patterns,值是文件的一些附加信息(例如,生成文件的“源”系统)。例如, KV("gs://bucket1/dir1/*"
我只想将多个字符数组作为成员数据存储在我的 FileIO 类的私有(private)字段中。出于某种原因,我可以有一个字符数组并且它工作正常,但是当我添加第二个时,我得到标题中所述的错误。 这段代码工
我知道 Python 中有 StringIO 流,但是 Python 中有文件流这样的东西吗?我还有更好的方法来查找这些东西吗?文档等... 我正在尝试将“流”传递给我制作的“作家”对象。我希望我可以
尝试使用 读取 locl 文本文件时出现以下异常 var text = await FileIO.ReadTextAsync(file); The handle with which this opl
在 Windows 8 WinRT 中,有一个方便的类 FileIO,可让您在一次异步调用中将 IStorageFile 插入内存。 WP8 不支持;是否有对应物,或者我应该通过流来做这件事吗?
我在 .NET Framework 4.0 上,在 VisualStudio 2012 中构建 C# Web 应用程序。我添加了 Microsoft.VisualBasic 作为对项目的引用。我在使用
我通常使用 C# 进行开发,并且非常习惯使用 System.IO。我最近浏览了 FileIO 库,发现其中一些优点(例如将项目发送到回收站)非常好。特别是在我目前正在从事的项目中。我的问题是,Syst
我是一名优秀的程序员,十分优秀!