ç解析输入文本文件导入的话 [英] C parsing input text file into words

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

问题描述

我试图解析输入文件(包含多行和分隔符的文本文件,即!,。?)进言。我的功能功能拆分是:

  INT splitInput(FP){    INT I = 0;
    焦线[255];
    的char *数组[5000];
    INT X;
    而(与fgets(线,的sizeof(线),FP)!= NULL){
        数组[我] = strtok的(行,\\ n。!?);
        的printf(检查打印 - 字%I:%S:\\ n,我,阵列[我]);
        我++;
    }
    返回0;
}


解决方案

下面是校正功能[对不起额外的样式清理]:

  INT
splitInput(FP)
{
    INT I = 0;
    字符* CP;
    字符*基点;
    焦线[255];
    的char *数组[5000];
    INT X;    而(与fgets(线,的sizeof(线),FP)!= NULL){
        BP =行;
        而(1){
            CP = strtok的(BP,\\ n。!?);
            BP = NULL;            如果(CP == NULL)
                打破;
            数组[我++] = CP;            的printf(检查打印 - 字%I:%S:\\ n,I-1,CP);
        }
    }    返回0;
}

现在,看看手册页 strtok的来理解 BP 诱骗

I am trying to parse input file (containing a text document with multiple lines and delimiters, i.e. "!,.?") into words. My function 'splitting function' is:

int splitInput(fp) {

    int i= 0;
    char  line[255];
    char *array[5000];
    int x;
    while (fgets(line, sizeof(line), fp) != NULL) {     
        array[i] = strtok(line, ",.!? \n");
        printf("Check print - word %i:%s:\n",i, array[i]);
        i++;
    }
    return 0;
}

解决方案

Here's the corrected function [sorry for extra the style cleanup]:

int
splitInput(fp)
{
    int i = 0;
    char *cp;
    char *bp;
    char line[255];
    char *array[5000];
    int x;

    while (fgets(line, sizeof(line), fp) != NULL) {
        bp = line;
        while (1) {
            cp = strtok(bp, ",.!? \n");
            bp = NULL;

            if (cp == NULL)
                break;
            array[i++] = cp;

            printf("Check print - word %i:%s:\n",i-1, cp);
        }
    }

    return 0;
}

Now, take a look at the man page for strtok to understand the bp trick

这篇关于ç解析输入文本文件导入的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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