Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我正在尝试读取文本文件的第一行,然后使用fgets跳过它,但这给我带来了段错误,有人可以帮助我吗?在添加fgets之前它已经起作用了,因此看来fgets是问题所在。



#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int N
    const int STACK_SIZE=65536;
    int col=0;
    int i;
    int j;
    FILE *file1;
    int s;
    int row=0;
    int prev='t';
    char m[1024];

    if(argc != 3)
    {
        fprintf(stderr, "Usage: %s <Executable> <Input file> <Threads>\n", argv[0]);
        exit(1);
    }

    file1=fopen(argv[1],"r");
    if(file1==NULL) //check to see if file exists
    {
        fprintf(stderr, "Cannot open %s\n", argv[1]);
        exit(1);
    }

    stack=malloc(STACK_SIZE);
    if(stack==NULL)
    {
        perror("malloc");
        exit(1);
    }

    if(atoi(argv[2]) == 0)
    {
        fprintf(stderr,"Threads has to be a number.\n");
        exit(1);
    }

    fscanf(file1,"%d",&N);
    rewind(file1);

    fgets(m,sizeof(m),file1);
    while((s=fgetc(file1)) != EOF)
    {
        if(s == ' ')
        {
            prev='a';
            continue;
        }
        if(s == '\n' && prev != '\n')
        {
            row++;
            if(col != N)
            {
                fprintf(stderr, "File %s has incorrect columns.\n", argv[1]);
                exit(1);
            }
            col=0;
            prev='a';
        }
        if(s != ' ' && s != '\n')
        {
            col++;
            prev='a';
        }
    }
    if(row != N)
    {
        fprintf(stderr,"File %s has incorrect rows.\n", argv[1]);
        exit(0);
    }
    rewind(file1);

    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            fscanf(file1,"%d",&A[i][j]);
        }
    }

    fclose(file1);
}
}


编辑1:
固定。代码放置是唯一的问题。

最佳答案

您无需检查文件是否已打开,fscanf()fgets()期望有效的FILE *您需要检查fopen()的返回值1,还应确保在命令行中提供了一个参数,用于您可以在以下代码中检查argc示例

int main(int argc, char **argv)
{
    char  buffer[1024];
    FILE *file;

    if (argc < 2)
    {
        fprintf(stderr, "you need to provide a file name.\n");
        return 1;
    }

    file = fopen(argv[1], "r");
    if (file == NULL) /* problem opening */
    {
        fprintf(stderr, "error openning %s\n", filename);
        return 2;
    }
    if (fgets(buffer, sizeof(buffer), file) != NULL)
        printf("%s\n", buffer);
    fclose(file); /* close the file */
    return 0;
}


即使使用错误的参数数量调用程序,程序无法打开文件或无法读取文件,上述代码也不会失败。



1. fopen()在无法打开文件时返回NULL

关于c - 使用fgets和fscanf时在C中出现段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28650819/

10-16 20:14