错误的文件输入c++
Incorrect file input c++
由于某些原因,这段代码
for (i = 0; i < 1024; i++)
mem[i] = 0;
//Read input file
instructions.open (fname.c_str(), fstream::in);
if (!instructions.is_open() ) {
std::cout << "Error 404: file not foundn";
exit (404);
}
for (i = initial_pos; !instructions.eof(); i++)
instructions >> mem[i];
正在读取这个文件
1
21
1
9
11
9
16
11
9
3
60
2
0
21
0
1
11
4
0
2
2
90
0
这样:
1
33
1
32
11
0
28
11
1
26
11
2
24
11
3
22
41
1
1
51
8
22
1
3
21
2
0
60
34
12
5
2
2
3
90
0
0
0
1
0
是否有什么特别的原因为什么>>操作数似乎是向mem添加随机数?请注意,mem是一个初始化的数组,所有的数字都是在读取后打印出来的。
冒着写第一百遍的风险,下面是你的代码应该是的样子:
std::ifstream infile(fname.c_str()); // "in" is implied
if (!infile) { /* error, could not open file */ }
for (int n; infile >> n; )
{
// we read the number n
}
如果你只想要一个整数的容器,那就更好了:
#include <vector>
#include <iterator>
#include <fstream>
std::ifstream infile(fname.c_str());
std::istream_iterator<int> beg(infile), end();
std::vector<int> v(beg, end);
// now "v" contains all the integers.
修复。我把可执行文件调用错了。
应该是:
[[[[输入名称]]]]
但是我是这样命名的:
sim test1.bin
因此模拟器将初始PC设置为"test1.bin",并将输入设置为默认输入"input.bin",因此我读取了错误的文件。
我想这总是归结为糟糕的家庭作业发音。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type