- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想生成一个随机数组。
void Random_nums(int n, string fileName) {
ofstream fsave(fileName);
int ranArray[n];
srand( (unsigned int)time(NULL) );
for(int i=0; i<n; i++)
{
ranArray[i]=rand()%2001 - 1000;
fsave << ranArray[i] << '\n'; //save them into ranArray
}
fsave.close();
}
我想将它们保存到一个单独的文件中。
之后我想读取刚刚保存的数据并在新函数中实现它们。
void FunctionWantToUse(string inputFile, string outputFile) {
//reading data
ifstream fin(inputFile);
cout << fin.is_open();//which is 1
//how many numbers are there in this file
int n = 0;
n = distance(istream_iterator<int>(fin), istream_iterator<int>());
//restore the array
int array[n];
//test
cout << fin.peek() << ' ' << EOF;//they are both -1!
//another test
cout << fin.peek() << '\n'; //which return -1
cout << fin.eof() << '\n'; //which return 1
//since peek() and EOF both return -1, I can't get into this loop.
while (fin.peek() != EOF) {
if ((fin >> array[i]).good()) {
cout << array[i];
}
}
完全不知道为什么 fin.peek() 和 fin.eof() 的值分别变为 -1 和 1。
当我运行这两个函数时,控制台告诉我这一点。
-1 -1-1
1
Process finished with exit code 0
我还给出了一些其他的测试,比如,当我生成随机数时,将 fsave.eof() 放在循环中,它打印出全 0。
有人能告诉我这是怎么回事吗?我现在完全不知道。
最佳答案
与 std::distance(istream_iterator<int>(fin), istream_iterator<int>())
你遍历文件直到它无法读取并且 int
.发生这种情况是因为有些输入无法解析为 int
(跳过前导空格后)或到达文件末尾。无论如何,当调用 distance()
返回流将有 std::ios_base::failbit
设置,如果错误是由于文件末尾也到达 std::ios_base::failbit
.
明显的解决方法是不确定距离,而是直接将整数读入合适的表示形式。例如:
std::vector<int> array{std::istream_iterator<int>(fin),
std::istream_iterator<int>()};
如果您真的想改用循环,您应该测试流在之后从流中读取是否处于失败状态,例如:
for (int tmp; fin >> tmp; ) {
// use tmp
}
当您真的觉得需要先确定元素的数量时,您需要
clear()
获得 std::ios_base::failbit
后的流状态放。否则,大多数流操作将被忽略。seekg()
回到流的开始。但是请注意,调整 std::vector<int>
的大小几次几乎肯定比通读一个文件并解析其内容两次更有效。
顺便说一句,你对数组的使用有两个问题:
你最好使用 std::vector<int>
相反。
关于c++ - 文件设置正确,但 eof() 和 peek() 意外从新流返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156731/
读取文件时,我知道提供的最后一个字符是 EOF。现在,当我有 EOF 时会发生什么?该文件中的字符? 如何区分文件的“真实”结尾和 EOF特点? 最佳答案 我决定将我的评论移至答案。 您的文件中不能有
GNU Bash - 3.6.6 Here Documents [n]<<[-]word here-document delimiter If any part of word is
试图查看论坛但无法重新访问。 我正在尝试阅读文本。文字是:“给 b” 但是在使用 fgetc() 时,没有达到 EOF,最后我得到 '\n',然后是无穷大的 'y' 样本。 这是我的代码: Node*
我正在做一个非常简单的作业,需要输出文件的内容,但在做任何事情之前我意外地到达了 EOF。该文件仅包含单词“pig”,并且由于某种原因 EOF 返回 16。我正在使用 Dev-Cpp 并且该程序是用
我的 kubectl 无法读取文件,每次都返回 'error: unexpected EOF'。 kubectl apply -f service.yaml > error: unexpected E
我想知道什么时候使用 cat Hello world! () { setopt localoptions shnullcmd test-nullcmd } # nothing will be
我试图找出我正在向其中写入特定数据的文件中不需要的尾随结束数据的原因,并且不相信我在写入文件时出错。 输出如下: building room_numbr capacity packard
考虑下面的简单例子 #include #include #include using namespace std; int main() { string str = "string";
我在一个程序中有这个片段(在 Visual Studio 2005 中): if(_eof(fp->_file)) { break; } 当达到 eof 时,它打破了封闭循环。但是程序无法解析
我正在尝试为 Typed Racket 中的以下函数定义类型注释: (define (neof x) (if (eof-object? x) #f x)) 不加注释给出了类型: (Any ->
我正在为 Linux 构建系统的模块编写 .spec 文件,遇到一个小问题并想分享它。 用于编写脚本文件: cat /path/to/somewhere/script #blah blah EOF
我有一个 C++ 程序,它从一个文件中读取我希望有一堆格式相同的记录。如果遇到意外情况,无论是记录格式不正确还是输入失败,我都想停止阅读,我想区分这些不同的情况。 我看过 this answer并查看
我注意到它们之间的几个区别: 在 <
预期我正在生成子进程并执行“ssh”远程框。从最近几天开始它工作正常,现在突然间,每当我尝试生成子进程时,它都会抛出错误。不确定发生了什么。 直到现在我一直在使用 pexpect 3.1,我遇到了这个
这个问题已经有答案了: Why two EOF needed as input? [duplicate] (2 个回答) Why do I require multiple EOF (CTRL+Z)
我正在浏览一个文件来寻找特定的词 Char[50]=getline(file,/n) 使用 getline 将每一行存储到一个 char 数组中以与我要查找的字符串进行比较 If( “”==char[
引用两个问题: Incorrect output from C++ Primer 1.4.4 Confused by control flow execution in C++ Primer exam
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我刚刚在 this 中找到一条评论回答说在循环条件中使用 iostream::eof “几乎肯定是错误的”。我通常使用类似 while(cin>>n) 的东西——我猜它会隐式检查 EOF。 为什么使用
我是一名优秀的程序员,十分优秀!