gpt4 book ai didi

c - 解决简单 C 扫描器中的 clang 警告

转载 作者:行者123 更新时间:2023-11-30 19:24:29 25 4
gpt4 key购买 nike

我有一个基本的 C 代码,旨在通过打开文件、读取内容和关闭文件来读取文件的内容。尽管以下代码可以编译并运行,但 clang 编译器会给出一些额外的警告,我无法解决这些警告。

#include <stdio.h>

char* scanFile(FILE *fl, char* ch);

int main(int argc, char const* argv[])
{
FILE *file;
char *ch = "\0";

ch = scanFile(file, ch);
fclose(file);

printf("%s\n", ch);
return 0;
}

char* scanFile(FILE *fl, char* ch)
{
fl = fopen("/tmp/test.txt", "r");
if (fl)
{
while ((ch = getchar()) != EOF) { putchar(ch); }
}

return ch;
}

当我尝试编译代码时,我的 clang 会闪烁以下警告。

clang scanFile scanFile.c 
scanFile.c:22:20: warning: incompatible integer to pointer conversion assigning to 'char *'
from 'int' [-Wint-conversion]
while ((ch = getchar()) != EOF) { putchar(ch); }
^ ~~~~~~~~~
scanFile.c:22:33: warning: comparison between pointer and integer ('char *' and 'int')
while ((ch = getchar()) != EOF) { putchar(ch); }
~~~~~~~~~~~~~~~~ ^ ~~~
scanFile.c:22:51: warning: incompatible pointer to integer conversion passing 'char *' to parameter of type 'int' [-Wint-conversion]
while ((ch = getchar()) != EOF) { putchar(ch); }
^~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdio.h:172:17: note: passing argument to parameter here
int putchar(int);
^
3 warnings generated.
ld: can't link with a main executable file 'scanFile' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,由于编译过程没有任何严重错误,因此当我运行上述编译结果生成的可执行文件时,我在屏幕上看到以下内容(以下人造拉丁语是应该是的文件)在我的 C 代码中读取并输出。)

Ergo ego senator inimicus, si ita vultis, homini, amicus esse, sicut semper fui, rei publicae debeo. 
Quid? si ipsas inimicitias, depono rei publicae causa, quis me tandem iure reprehendet, praesertim cum ego omnium meorum consiliorum atque factorum exempla semper ex summorum hominum consiliis atque factis mihi censuerim petenda.

Eodem tempore etiam Hymetii praeclarae indolis viri negotium est actitatum, cuius hunc novimus esse textum.
Cum Africam pro consule regeret Carthaginiensibus victus inopia iam lassatis, ex horreis
Romano populo destinatis frumentum dedit, pauloque postea cum provenisset segetum copia, integre sine ulla restituit mora.

Et hanc quidem praeter oppida multa duae civitates exornant Seleucia opus Seleuci regis, et Claudiopolis quam deduxit coloniam Claudius Caesar.
Isaura enim antehac nimium potens, olim subversa ut rebellatrix interneciva aegre vestigia claritudinis pristinae monstrat admodum pauca.
[1] 59823 segmentation fault ./scanFile

注意:但它仍然给出“59823段错误./scanfile”错误。

额外:clang -Wall 给出;

C clang -Wall scanFile.c 
scanFile.c:10:19: warning: variable 'file' is uninitialized when used here [-Wuninitialized]
ch = scanFile(file, ch);
^~~~
scanFile.c:7:15: note: initialize the variable 'file' to silence this warning
FILE *file;
^
= NULL
scanFile.c:22:20: warning: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]
while ((ch = getchar()) != EOF) { putchar(ch); }
^ ~~~~~~~~~
scanFile.c:22:33: warning: comparison between pointer and integer ('char *' and 'int')
while ((ch = getchar()) != EOF) { putchar(ch); }
~~~~~~~~~~~~~~~~ ^ ~~~
scanFile.c:22:51: warning: incompatible pointer to integer conversion passing 'char *' to parameter of type 'int' [-Wint-conversion]
while ((ch = getchar()) != EOF) { putchar(ch); }
^~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdio.h:172:17: note: passing argument to parameter here
int putchar(int);
^
4 warnings generated.

最佳答案

Ahmet 你的代码中有一些错误:

  • 您没有为结果字符串分配内存。当您像 char *ch = "\0"; 那样定义字符串时,它被称为字符串文字。尝试修改字符串文字是未定义的行为。您可以在 here 阅读有关字符串文字的更多信息。 。这会给你带来段错误。
  • 您正在尝试将文件指针传递给 scanFile 函数,以便在函数完成时关闭文件。但是,当您将文件指针传递给 scanFile 函数时,它是按值传递的。编译器警告您在传递它之前没有初始化它。当 scanFile 函数打开文件并将生成的文件指针分配给其参数时,无法在函数外部访问该文件。您必须返回一个文件指针才能执行此操作。
  • 您正在使用 getchar 从文件中读取字符。它用于从stdin获取字符。默认情况下它从键盘获取字符。您应该使用 fgetc 从文件中获取字符。

这里是一个工作示例,已更新,用于检查 errno 是否可能出现 mallocfopen 失败:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define MAX_BUFFER_SIZE 1000

struct {
FILE *fPtr;
char *text;
} typedef Result;

Result scanFile(const char *fileName);

int main(int argc, char const* argv[]) {
Result result = scanFile("text.txt");
printf("Result:\n%s\n", result.text);
return 0;
if (errno != 0) {
printf("Program ended with error: %d\n", errno);
return -1;
}

printf("Result:\n%s\n", result.text);
// Close the file
fclose(result.fPtr);
// Free buffer
free(result.text);
return 0;
}

Result scanFile(const char *fileName) {

Result result;

result.text = (char *)malloc(sizeof(char)*MAX_BUFFER_SIZE);

if (result.text == NULL) {
printf("scanFile: Faield to allocate memory!\n");
printf("scanFile: %s\n", strerror( errno ));
return result;
}

result.fPtr = fopen(fileName, "r");

if (result.fPtr == NULL) {
printf("scanFile: Faield to open file!\n");
printf("scanFile: %s\n", strerror( errno ));
return result;
}

unsigned int count = 0;
int ch;

while ((ch = fgetc(result.fPtr)) != EOF) {
if (count < MAX_BUFFER_SIZE) {
result.text[count] = ch;
++count;
} else {
printf("scanFile: Exceeded buffer size!\n");
break;
}
}

return result;
}

关于c - 解决简单 C 扫描器中的 clang 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60256989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com