在C中解析.txt文件 [英] Parsing .txt file in C

查看:265
本文介绍了在C中解析.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析C语言中的.txt文件.这是我到目前为止所拥有的

i am trying to parse through a .txt file in C. This is what i have so far

void parser()
{
FILE * rooms;
char * theString;
char * theToken;

char a[ROOM_STRING_LENGTH];

rooms = fopen("assets/rooms.txt", "r");

if(rooms == NULL)
{
    printf("error opening file\n");
}
while(fgets(a, ROOM_STRING_LENGTH, rooms) != NULL)
{

    theString = malloc((sizeof(char)*(strlen(a)+1)));
    strcpy(theString, a);
    theToken = strtok(theString, " ");
    while (theToken != NULL)
    {
        printf("the next token: %s\n", theToken);
        theToken = strtok(NULL, " ");
        if(theToken[0] == 'd')
        {
            doorParser(theToken);       
        }

        else if(theToken[0] == 'g' || theToken[0] == 'm' || theToken[0] == 'p' || theToken[0] == 'h')
        {
             iconParser(theToken);
        }
    }
    if(theToken == NULL)
    {

    }

}
free(theString);
fclose(rooms);
}
void iconParser(char * theToken)
{
int k;
int item;
char posX;
char posY;
while(k <= (strlen(theToken)))
{
    switch(theToken[k])
    {
        case 'g':
        item = 1;
        posY = theToken[1];
        posX = theToken[3];
        printf("the item: %d, the y position: %c, the x position: %c\n", item, posY, posX);
        break;

        case 'm':
        item = 2;
        posY = theToken[1];
        posX = theToken[3];
        break;

        case 'p':
        item = 3;
        posY = theToken[1];
        posX = theToken[3];
        break;

        case 'h':
        item = 4;
        posY = theToken[1];
        posX = theToken[3];
        break;
    }
    k++;
}
}


void doorParser(char * theToken)
{
int side;
char place;
switch(theToken[1])
{
    case 'e':
    {
        side = 1;
        place = theToken[2];
        printf("the side: %d, the place: %c\n", side, place);
        break;
    }
    case 'w':
    {
        side = 2;
        place = theToken[2];
        break;
    }
    case 's':
    {
        side = 3;
        place = theToken[2];
        break;
    }
    case 'n':
    {
        side = 4;
        place = theToken[2];
        break;
    }
    default:
    {

    } 
} 
}

这是我的.txt文件:

This is my .txt file:

12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,2
12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,4
12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,6
12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,10
12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,12
12X6 de8 dw3 ds5 g8,7 m3,4 p2,2 h2,14

我现在遇到的问题是,在它运行一行或.txt文件之后,我遇到了段错误,而且我想知道如何转换字符串的某个值(即char)转换为int值

The problem i am having right now, is that after it runs through one line or and then of the .txt file then i get a segmentation fault, Also i am wondering how i would go about converting a certain value of a string(that is a char) into an int value

推荐答案

一方面,循环的结构没有任何意义:

For one thing, the structure of your loop doesn't make any sense:

while (theToken != NULL)
{
    printf("the next token: %s\n", theToken);
    theToken = strtok(NULL, " ");
    if(theToken[0] == 'd')  <<< This is dereferencing NULL after last token!

您正在检查theToken的条件是否不是NULL,然后获得下一个令牌并立即使用它(通过尝试查找其第0个字符),即使它是.

You are checking that theToken is not NULL with the while condition, then you get the next token and use it immediately (by trying to look up its 0'th character) even if it is NULL.

使用strtok的惯用方式是

for (tok = strtok(str, " "); tok; tok = strtok(NULL, " "))
{
   // Process the token here.

这篇关于在C中解析.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆