在空白行中打印变量结果

时间:2018-12-04 22:44:51

标签: c structure c99

从已解析的文件返回包含所有信息的结构后,除结构中不是指针的一个数据成员外,所有数据成员均被打印为空白行。我认为这是处理指针的问题,但我不确定。例如,我在结构中设置了一个char *,使其等于文件中的名称。设置完变量后,立即将其打印出来,并且名称正确,但是一旦我将指针返回到该结构,或者一旦我位于读取文件的while循环之外,当我尝试打印名称。任何帮助都将是惊人的。谢谢。

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

#define _DEFAULT_MAP_SIZE 10
#define _MAX_SIZE 80

typedef struct Map_s{

        char * defender;
        char * attacker;
        int missles;
        int ** layout;
        size_t capacity;
        size_t size;

}map;

第一段代码显示了我包括的文件头以及将要使用的结构。

map * newGame(){

        map * game = malloc(sizeof(map));
        game->layout = (int**)malloc(_DEFAULT_MAP_SIZE * sizeof(int*));
        game->defender = NULL;
        game->attacker = NULL;
        game->missles = 0;
        game->capacity = _DEFAULT_MAP_SIZE;
        game->size = 0;

        return game;
}

用于初始化新结构类型的函数

map * parseFile( FILE * fp ){

        map * game = newGame();

        char * token;
        const char delim[2] = " ";
        char * buf = NULL;
        char * temp = NULL;
        size_t size = _MAX_SIZE;
        int lineNum = 1;

        while( getline(&temp, &size, fp) > 1 ){
                buf = strtok(temp, "\n");
                //printf("%s\n", buf);
                if( buf[0] != '#' ){
                        printf("%s\n", buf);
                        printf("%d\n", lineNum);
                        if( lineNum == 1){
                                if( game->defender == NULL ){
                                        game->defender = buf;
                                }
                        }
                        else if( lineNum == 2 ){
                                if( game->attacker == NULL )
                                        game->attacker = buf;
                        }
                        else if( lineNum == 3 ){
                                game->missles = atoi(buf);
                        }
                        else if( lineNum > 3 ){
                                token = strtok(buf, delim);

                                while( token != NULL ){
                                        if( game->size >= game->capacity ){
                                                game->layout = (int**)realloc(game->layout, \
                                                                game->capacity*2 * sizeof(int*));
                                                game->capacity = game->capacity * 2;
                                        }

                                        game->layout[game->size] = (int*)token;
                                        game->size = game->size + 1;
                                        token = strtok(NULL, delim);
                                }
                        }

                        lineNum++;
                }

        }

        return game;

}

此代码解析文件。以“#”开头的任何行都是注释,将不被使用。文件的前两行是名称,第三行是数字,其后任何一行将是十个整数,结尾处带有换行符。

1 个答案:

答案 0 :(得分:0)

您正在将读取缓冲区的地址存储到“ map”结构,其内容将在每次getline()调用时更新。您可以通过以下更改来解决此问题

while( getline(&temp, &size, fp) > 1 ){
                buf = strtok(temp, "\n");
                //printf("%s\n", buf);
                if( buf[0] != '#' ){
                        printf("%s\n", buf);
                        printf("%d\n", lineNum);
                        if( lineNum == 1){
                                if( game->defender == NULL ){
                                        line_end = strchrnul(buf,'\n');
                                        line_size = line_end - buf;
                                        game->defender = malloc(line_size + 1);
                                        strncpy(game->defender,buf,line_size;
                                }
                        }
                        else if( lineNum == 2 ){
                                if( game->attacker == NULL ) {
                                        line_end = strchrnul(buf,'\n');
                                        line_size = line_end - buf;
                                        game->attacker = malloc(line_size + 1);
                                        strncpy(game->attacker,buf,line_size;
                                }
                        }
                        else if( lineNum == 3 ){
                                game->missles = atoi(buf);
                        }
                        else if( lineNum > 3 ){
                                token = strtok(buf, delim);

                                while( token != NULL ){
                                        if( game->size >= game->capacity ){
                                                game->layout = (int**)realloc(game->layout, \
                                                                game->capacity*2 * sizeof(int*));
                                                game->capacity = game->capacity * 2;
                                        }

                                        game->layout[game->size] = malloc(sizeof(int));
                                        *game->layout[game->size] = *(int*)token;
                                        game->size = game->size + 1;
                                        token = strtok(NULL, delim);
                                }
                        }

                        lineNum++;
                }

        }