简体   繁体   English

为什么我会出现段错误?

[英]Why am I segfaulting?

I'm very new to C, I am attempting to read the contents of one file character by character and output them to the stream.我对 C 很陌生,我试图逐个字符地读取一个文件的内容并将它们输出到流中。 But even with my fopen() command commented out I receive segfault (core dumped).但即使我的 fopen() 命令被注释掉,我也会收到段错误(核心转储)。

I must run a command: ./a.out < testWords.in > myOut.txt to execute my file properly.我必须运行一个命令:./a.out < testWords.in > myOut.txt 才能正确执行我的文件。

Here is what I have so far:这是我到目前为止所拥有的:

#include  <stdio.h>
void main(char *fileName[]) 
{
  printf("filename is %s.\n",fileName[0]);
  //Get file based on a string inputed
  FILE *fp=fopen(fileName[0],"r"); //Fetches our file as read only
  char ch;
  int lineCount = 0;
  int wordCount = 0;
  int charCount = 0;

  //Failed to find/open file. NULL character.
  if (fp == 0) printf("Woops! Couldn't open file!\n");

  //While not at end of file, grab next char.
  else while( (ch=fgetc(fp)) != EOF) 
    {
      if (ch == '\n') //on newline
      {
    //Prints (charCount,wordCount)\n lineCount:
    printf("(%d,%d)%c%d:",charCount,wordCount,ch,lineCount);
    charCount = 0;
    lineCount += 1;
      }
      else printf("%c",ch); //mirrors char.
    }
  fclose(fp); //Closes file (gotta be tidy!)

}

You can't just invent a way to call main .您不能仅仅发明一种调用main You need to use one of the standard ways, like this:您需要使用其中一种标准方式,如下所示:

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stderr, "Missing filename\n");
        return -1;
    }
    FILE *fp = fopen(argv[1], "r");
    // ...
}

And note that argv[0] contains the program name (if available; if not it contains an empty string).并注意argv[0]包含程序名称(如果可用;如果不可用,则它包含一个空字符串)。

Your program segfaulted because you received the int argc argument into your char *filename[] parameter.您的程序出现段错误,因为您在char *filename[]参数中收到了int argc参数。 If you ran the program with a single command line parameter, the value passed in as the first argument would have been 2, which is not a valid pointer value.如果您使用单个命令行参数运行程序,则作为第一个参数传入的值将是 2,这不是有效的指针值。 The expression filename[0] dereferences that address and causes a segfault.表达式filename[0]取消引用该地址并导致段错误。

Any time you get a segfault in C, you should smell a bad pointer or address in an argument list.每当您在 C 中遇到段错误时,您都应该在参数列表中到错误的指针或地址。 In this particular case., the signature of main is always int main(int argc, char** argv) .在这种特殊情况下, main 的签名始终int main(int argc, char** argv) Yours isn't.你的不是。

What you want is你想要的是

int main(int argc, char ** argv) {
  ...
  FILE * fp = fopen(argv[1]); // Quiz: why argv[1]? What's argv[0]?

You're getting away with it in the compiler because, basically, luck.你在编译器中逃脱了它,因为,基本上,运气。

I also notice in your example call, there's actually no argument in the argument list, because you're using redirection.我还注意到在您的示例调用中,参数列表中实际上没有参数,因为您使用的是重定向。

Use:用:

int main(int argc, char* argv[])

And use argv[1] as fileName.并使用 argv[1] 作为文件名。

Main function must receive always that two parameters.主函数必须始终接收那两个参数。

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

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