文件输入错误
File input Error
我正试图在我的代码中输入一个文件,该文件包含一个带有换行符的整数列表我使用以下代码输入到文件中的内容
int main()
{
FILE* f = fopen("Integers.txt", "r");
int n = 0, i = 0;
int numbers[5]; // assuming there are only 5 numbers in array input
while( fscanf(f, "%dn", &n) > 0 ) // parse %d followed by 'n'
{
numbers[i++] = n;
}
fclose(f);
}
我有分段错误,所以请帮忙。
试试这个。。。。。
main()
{
FILE* f = fopen("Integers.txt", "r");
int n,i;
int numbers[5]; // assuming there are only 5 numbers in array input
for (i=0; i<5; i++)
{
fscanf(f, "%d", &n)
numbers[i] = n;
}
fclose(f);
}
您可能应该尝试一下,因为数组numbers
只是5
。
int main()
{
int n = 0, i = 0;
int numbers[5]; // assuming there are only 5 numbers in array input
FILE* f = fopen("Integers.txt", "r");
if (f != NULL)
{
for (i=0; i<(sizeof(numbers)/sizeof(int)); i++)
{
if (fscanf(f, "%dn", &n) > 0)
numbers[i] = n;
else
break;
}
fclose(f);
}
}
数组numbers[5]
在堆栈中,越界访问它将损坏堆栈。
相关文章:
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 堆栈中大小变量输入错误 (C++)
- 实现 DFS 在较短的输入下工作正常,但在较大的输入下会抛出分段错误
- C++中的输入限制和错误检查
- 关于 SIGTSTP 错误和无休止的输入
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 这段代码在获得错误输入时不会阻止 stoi 运行
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 使用 cin 错误输入多个浮点数
- 构造函数错误:输入末尾的预期“{”
- 如何一次一个单词而不是一行一行地消除错误输入
- 错误输入未在此范围中显示
- c++在没有tryCatch块的情况下捕获错误输入
- c++中的纠错循环,查找字符串中的特定字符并将其标记为错误输入
- 如何为错误输入编写异常处理
- Swig错误-错误:输入(2)中的语法错误
- 错误:输入末尾应为“}”
- std::cin无限循环错误输入
- 如何避免来自用户的错误输入?