简体   繁体   English

读文件会导致段错误

[英]Reading in file leads to seg fault

So I've got a text file: 所以我有一个文本文件:

5f6
2f8
2f2

And I'm reading in the values : 5,6,2,8,2,2 where the first two numbers are always rows x columns, then I'm trying to draw rectangles in retrospect to the files values (This works, but after running the program and when it prints them, it seg faults). 我正在读取以下值:5,6,2,8,2,2,其中前两个数字始终是行x列,然后我试图回想起对文件值绘制矩形(这可行,但是在运行该程序并打印它们之后,将出现段错误)。

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

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

    initscr();
    cbreak();
    noecho();

    char str[100];
    static char * row;
    static char * col;
    static int i;
    static int j;
    static int k;
    static int x; 
    static int y;
    int count = 0;

    FILE* file;
    file = fopen(argv[1],"r");

    if(file == NULL)
    {
        mvprintw(0,0,"File returns null pointer");
        exit(1);
    }

    while(!feof(file))
    {
        fgets(str,100,file);
        row = strtok(str,"x");
        col = strtok(NULL," \n");

        x = atol(row);
        y = atol(col);

        for(j=0;j<x;j++)
        {
            for(k=0;k<y;k++)
            {
                mvprintw(k+5,j+5+count,".");
                refresh(); //Space out drawing each rectangle? so they don't overlap
            }
        }
        count+=5;
    }

    fclose(file);
    getch();
    endwin();
    return (0);
}

I'm not really sure on how to proceed here, how would I go about eliminating this seg fault, and possibly spacing out the resultant drawing (The count variable doesn't seem to do it). 我不太确定如何进行此操作,如何消除此段错误,并可能将生成的图形间隔开(count变量似乎不执行此操作)。

Your segmentation fault is because you are not checking whether strtok() returned NULL and you still dereference the pointer with atol() , this is an example of how to do it correctly 您的分段错误是因为您没有检查strtok()是否返回NULL并且仍使用atol()取消引用指针,这是如何正确执行此操作的示例

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

int main(int argc, char * argv[])
{
    char  str[100];
    int   j;
    int   k;
    int   count;
    FILE *file;

    file = fopen(argv[1],"r");
    if (file == NULL)
     {
        mvprintw(0, 0, "File returns null pointer");
        return 1;
     }

    initscr();
    cbreak();
    noecho();

    count = 0;
    while(fgets(str, sizeof(str), file) != NULL)
     {
        char *endptr;
        char *row;
        char *col;
        int   x;
        int   y;

        row = strtok(str, "f");
        if (row == NULL)
         {
            fprintf(stderr, "`%s' does not contain `f'\n", str);
            continue;
         }
        col = strtok(NULL, " \n");
        if (col == NULL)
         {
            fprintf(stderr, "`%s' caused an unexpected error\n", str);
            continue;
         }
        x = strtol(row, &endptr, 10);
        if (*endptr != '\0')
         {
            fprintf(stderr, "Cannot convert `%s' to integer\n", row);
            continue;
         }
        y = strtol(col, &endptr, 10);
        if (*endptr != '\0')
         {
            fprintf(stderr, "Cannot convert `%s' to integer\n", col);
            continue;
         }
        for (j = 0 ; j  < x ; j++)
         {
            for (k = 0 ; k < y ; k++)
             {
                mvprintw(k + 5, j + 5 + count, ".");
                refresh();
             }
         }
        count+=5;
     }

    fclose(file);
    getch();
    endwin();

    return 0;
}

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

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