简体   繁体   English

为什么我的另一个文件中的 void 函数没有在这个 C 程序中运行?

[英]Why my void function from another file is not running in this C program?

I want to print the content of a txt file (first parameter), but the function to do so is in a different file.我想打印 txt 文件的内容(第一个参数),但执行此操作的函数位于不同的文件中。 I have the following main file:我有以下主文件:

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include "fileoperation.h"

int main(int argc, char **argv)
{
  read(argv[1]);  
    
  return 0;
}

and then in the fileoperation.c file I have:然后在 fileoperation.c 文件中我有:

#include "fileoperation.h"

void read(char* file)
{
  FILE *fptr;
  char c;
  fptr = fopen(file, "r");
  if (fptr == NULL)
  {
    printf("Cannot open file \n");
    exit(0);
  }

  c = fgetc(fptr);
  while (c != EOF)
  {
    printf ("%c", c);
    c = fgetc(fptr);
  }
  
  fclose(fptr);

}

If I type the code from the function in the main function, it works.如果我在主函数中键入函数中的代码,它就可以工作。 I don't understand why is not working我不明白为什么不起作用

The header file of fileoperation.c is fileoperation.c 的头文件是

#ifndef FILEOPERATION_H
#define FILEOPERATION_H
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

void read(char* file);

#endif

Rename your function.重命名您的函数。 read exists in the backing libraries. read存在于支持库中。 To make matters worse, the compiler knows what it does and optimizes it out.更糟糕的是,编译器知道它做什么并优化它。

Could have been worse.本来可以更糟。 You could have replaced the actual read with your own and blown up the standard libraries.您可以用自己的替换实际read并炸毁标准库。

You appear to be using a non-standard compiler, or you have configured it incorrectly.您似乎使用的是非标准编译器,或者您的配置不正确。 Compilers like gcc and clang are known to invoke non-compliant behavior unless you compile with -std=c17 -pedantic-errors (see What compiler options are recommended for beginners learning C? ).众所周知,gcc 和 clang 等编译器会调用不合规的行为,除非您使用-std=c17 -pedantic-errors编译(请参阅为初学者学习 C 推荐哪些编译器选项? )。 I cannot reproduce the problem using a C compliant compiler.我无法使用 C 兼容编译器重现该问题。

A compliant compiler is not allowed to place non-standard identifiers in standard headers other than those explicitly reserved by the C standard.甲兼容的编译器是不允许放置非标准标识符在比那些由C标准明确地保留其他标准头。 Examples of reserved identifiers are those with two leading underscores, those with a leading underscore followed by an upper-case letter and identifiers reserved for future language/library extensions.保留标识符的例子是那些带有两个前导下划线的标识符,那些带有前导下划线后跟一个大写字母的标识符以及为将来的语言/库扩展保留的标识符。 Source: ISO 9899:2018 chapters 4/6, 7.1.3, 6.11 and 7.31.来源:ISO 9899:2018 第 4/6、7.1.3、6.11 和 7.31 章。

The mentioned compilers should remove all non-standard identifier declarations when compiling in C language compliant mode as described above.如上所述,在 C 语言兼容模式下编译时,上述编译器应删除所有非标准标识符声明。 If it doesn't, you have found a compiler library bug.如果没有,则您发现了编译器库错误。

There exists an old, well-known (and incredibly poorly named) function read , but it has never been part of the standard C library.存在一个古老的、众所周知的(而且名称非常糟糕)函数read ,但它从未成为标准 C 库的一部分。 (POSIX tried to standardize it but that's irrelevant here.) So the easy solution is to simply not name an identifier read - not so much because a well-known non-standard function has that name, but because it is a very poor and non-descriptive identifier name. (POSIX 试图对其进行标准化,但这在此处无关紧要。)因此,简单的解决方案是简单地不命名标识符read - 不是因为众所周知的非标准函数具有该名称,而是因为它非常糟糕且非- 描述性标识符名称。 In your case you could have used read_file or similar.在您的情况下,您可以使用read_file或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM