如何在 c++ 中将数字从文件移动到数组中
How to move numbers from a file into an array in c++
我在调用我创建的名为"numbers.txt"的文件中的数据时遇到问题。该文件具有数字 1-26,应该放在一个数组中。现在它没有编译。我有类似的项目,没有问题。所以我不知道我做错了什么。下面是我正在使用的代码。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// function prototypes:
int readEncodingCipher(string filename, int encodeKey[], int size);
int main()
{
string fileName;
const int size = 26;
int encodeKey[size];
//Requests the name of a file to read from.
cout << "Please enter a file name with a key: ";
cin >> fileName;
readEncodingCipher(fileName, encodeKey, size);
system("pause");
return 0;
}
int readEncodingCipher(string fileName, int encodeKey[], int size)
{
string fileName;
ifstream inFile;
int num;
int counter = 0;
inFile.open(fileName);
if (inFile)
{
while (inFile >> num && counter <= size)
{
encodeKey[counter] = num;
counter++;
}
}
else
{
cout << "unable to locate file";
}
inFile.close();
}
我假设您收到错误消息
error: declaration of 'std::__cxx11::string fileName' shadows a parameter
在你函数中
int readEncodingCipher(string fileName, int encodeKey[], int size)
{
string fileName;
局部变量fileName
遮蔽参数filename
。您必须更改变量名称。修复此错误后,它会为我编译。
此外,还应修复函数的返回类型。将其更改为void
或返回一个值。
您应该修复 while 循环的计数器。最后一个元素是 encodeKey[size - 1]
,所以 while 循环应该在这个元素处停止。
相关文章:
- 如何在文件中向下移动?
- Windows C++:文件夹移动访问被拒绝错误
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 通过从文件中读取值来计算移动平均线
- 正在将助手函数移动到头文件
- 如何将文件移动到Qt中的另一个现有目录
- 如何在 c++ 中将数字从文件移动到数组中
- 将文件移动到其他目录
- 将可执行文件移动到 make 文件中的其他目录后运行可执行文件
- 使用WXWIDGET将多个文件移动到循环时
- 一些奇怪的行为:将文件夹中的所有文件移动到另一个位置
- 文件移动 - 操作系统如何知道是更新主文件表还是复制和删除
- Qmake Qt:将头文件移动到构建文件夹
- 将 QFile::copy 创建创建文件的副本或将内容从一个文件移动到另一个文件
- 如何使用C++(VS)将n个第一个文件移动到另一个目录
- 将清单文件移动到dll
- 使用make将.o文件移动到一个单独的目录
- 将内联方法从头文件移动到.cpp文件
- 自动将函数从头文件移动到cpp文件
- 在Linux中使用c++将文件移动到垃圾桶