C++从文件中读取数字
C++ read number from a file
我有这个程序:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char** argv)
{
ifstream infile("argv[1]");
int testNumber = 0;
infile >> testNumber;
cout << testNumber << 'n';
}
我用这个命令./program myfile.txt
运行它
myfile.txt
包含一些由空格和新行分隔的整数
我希望程序输出文件中的第一个数字,但它总是打印0
。我做错了什么?
ifstream infile("argv[1]");
查找名为argv[1]
而非myfile.txt
的文件。要使用程序参数,请删除引号:
ifstream infile(argv[1]);
相关文章:
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 如何从文件中读取数字并在数组中使用它们?
- 使用模板化函数从CSV文件中读取数字
- 根据数字在 c++ 中作为矩阵的位置从文件中读取数字
- 如何从文件中读取数字序列?
- 如何允许我的程序成功读取数字包含的文件
- 如何仅从C 中的文本文件中读取数字
- 从没有分隔符 C++ 的文件读取数字出现次数
- 读取数字并转换为单词
- 从文件,特定行中仅读取数字
- 从TextFile中读取数字,并检查它们是否在范围内
- C 从文本文件中读取数字,忽略注释
- 从文件读取数字列表到动态数组
- 我正在尝试从文件中读取数字
- 从txt文件中读取数字,添加一些数字,然后在c ++中将其重写为其他文件
- C++如何从文件中读取数字并将其分配到数组中的相应位置
- 读取数字的文本文件,并存储在C++的整数数组中
- 从文件中读取数字
- 从文本中读取数字并输出平均值