【发布时间】:2023-02-25 04:19:12
【问题描述】:
我正在研究一个应该读取文件的函数,我需要将文本文件的第一行转换为整数。该函数将文件作为参数,char *filename。
但是,我在打开文件时遇到错误。
错误如下:“传递 'fopen' 的 2 个参数使指针来自整数而不进行强制转换 [-Wint-conversion] gcc”
FILE *fp = fopen(filename, 'r'); //Line with error
char str[6]; //since the first line is a 5 digit number
fgets(str, 6, fp);
sscanf(str, "%d", *number); //number is the pointer I'm supposed to save this value to, it is also a parameter for the function
我是 C 的新手。所以,我将不胜感激任何帮助。谢谢
【问题讨论】:
-
你写了 ' 而不是 "
-
我尝试将其更改为“,但仍然出现错误。
-
@shari 什么错误?
fopen(filename, "r")应该有效,假设filename有效。 (一般来说,“错误”几乎是对任何错误最无用的描述。我们需要查看消息和导致它的代码。)我希望你的sscanf调用出现错误,正如 Vlad 的回答所提到的.
标签: c compiler-errors scanf fopen fgets