为什么这段代码不打印文件的所有行

时间:2014-01-20 02:58:05

标签: c

我有下一个文本文件,我必须阅读然后打印

ADCA    SI  IMM     89ii        1   1   2
            DIR     99dd        1   1   2
            EXT     B9hhll      1   2   3
            IDX     A9xb        1   1   2
            IDX1    A9xbff      1   2   3
            IDX2    A9xbeeff    1   3   4
            [D,IDX] A9xb        1   1   2
            [IDX2]  A9xbeeff    1   3   4

但不是打印整个文件,而是打印:

ADCA    SI  IMM     89ii        1   1   2         
            EXT     B9hhll      1   2   3             
            IDX1    A9xbff      1   2   3            
            [D,IDX] A9xb        1   1   2

有一些行丢失,我不明白为什么,但如果我在行的末尾添加一些制表符,它可以正常工作,我怎么能解决这个没有\ t?这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 8

typedef enum {INS,OP,DIR,MAQ,CAL,X_CAL,TOTAL} table;

void remove(char *c);
void SearchEndLine(FILE *fd);
void ignoreSpaces(FILE *fd);
char *Operands(FILE *hc12,int table);

int main()
{
    int car,i;
    FILE *hc12;
    char *ins,*op,*dir[MAX],*maq[MAX],*cal[MAX],*x_cal[MAX],*s[MAX];
    if((hc12 = fopen("file.txt","r"))!= NULL)
    {
        i = 0;
        while((car = fgetc(hc12))!= EOF)
        {
            if(car != '\t')
            {
                ins = Operands(hc12,INS);
                printf("%s\t",ins);
                ignoreSpaces(hc12);
                op = Operands(hc12,OP);
                printf("%s\t",op);
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12,DIR);
                printf("%s\t",dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12,MAQ);
                printf("%s\t",maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12,CAL);
                printf("%s\t",cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12,X_CAL);
                printf("%s\t",x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12,TOTAL);
                printf("%s\n",s[i]);
                SearchEndLine(hc12);
            }
            else
            {
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12,DIR);
                printf("\t\t%s\t",dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12,MAQ);
                printf("%s\t",maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12,CAL);
                printf("%s\t",cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12,X_CAL);
                printf("%s\t",x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12,TOTAL);
                printf("%s\n",s[i]);
                SearchEndLine(hc12);
            }
            i++;
        }
    }
    return 0;
}

void SearchEndLine(FILE *fd)
{
    int car;
    while((car = fgetc(fd))!= '\n')
        ;
}

void ignoreSpaces(FILE *fd)
{
    int car;
    do
    {
        car = fgetc(fd);
    }while(car == '\t' || car == ' ');
}

char *Operands(FILE *hc12,int table)
{
    int car,lon = 0,pos;
    char *c;
    fseek(hc12,-1,SEEK_CUR);
    pos = ftell(hc12);
    if((table==INS)||(table==OP)||(table==DIR)||(table==MAQ)||(table==CAL)||(table==X_CAL))
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        }while(car != '\t');
    }
    else
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        }while(car != '\n');
    }
    fseek(hc12,pos,SEEK_SET);
    c = (char*)calloc((lon+1),sizeof(char));
    fgets(c,lon+1,hc12);
    remove(c);
    return c;
}

void remove(char *c)
{
    char *ptr;
    if(((ptr=strchr(c,'\n'))!=NULL)||((ptr=strchr(c,'\t'))!=NULL)||((ptr=strchr(c,' '))!=NULL))
       *ptr = '\0';
}

1 个答案:

答案 0 :(得分:1)

问题出在Operands()函数中;它会在一行上的最后一个字段之后咀嚼换行符,然后对SearchEndLine()的后续调用会占用下一行数据。你必须弄清楚如何避免这种情况。

此代码演示了该问题(它是代码的温和检测版本):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 8

typedef enum { INS, OP, DIR, MAQ, CAL, X_CAL, TOTAL } table;

void delete(char *c);
void SearchEndLine(FILE *fd);
void ignoreSpaces(FILE *fd);
char *Operands(FILE *hc12, int table);

int main(void)
{
    int car, i;
    FILE *hc12;
    char *ins, *op, *dir[MAX], *maq[MAX], *cal[MAX], *x_cal[MAX], *s[MAX];
    if ((hc12 = fopen("file.txt", "r")) != NULL)
    {
        i = 0;
        while ((car = fgetc(hc12)) != EOF)
        {
            if (car != '\t')
            {
                printf("Non-tab: ");
                ins = Operands(hc12, INS);
                printf("%s\t", ins);
                ignoreSpaces(hc12);
                op = Operands(hc12, OP);
                printf("%s\t", op);
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12, DIR);
                printf("%s\t", dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12, MAQ);
                printf("%s\t", maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12, CAL);
                printf("%s\t", cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12, X_CAL);
                printf("%s\t", x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12, TOTAL);
                printf("%s\n", s[i]);
                SearchEndLine(hc12);
            }
            else
            {
                printf("Tab:     ");
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12, DIR);
                printf("\t\t%s\t", dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12, MAQ);
                printf("%s\t", maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12, CAL);
                printf("%s\t", cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12, X_CAL);
                printf("%s\t", x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12, TOTAL);
                printf("%s\n", s[i]);
                SearchEndLine(hc12);
            }
            i++;
        }
    }
    return 0;
}

void SearchEndLine(FILE *fd)
{
    int car;
    while ((car = fgetc(fd)) != '\n')
        ;
}

void ignoreSpaces(FILE *fd)
{
    int car;
    do
    {
        car = fgetc(fd);
    } while (car == '\t' || car == ' ');
}

char *Operands(FILE *hc12, int table)
{
    int car, lon = 0, pos;
    char *c;
    fseek(hc12, -1, SEEK_CUR);
    pos = ftell(hc12);
    if ((table == INS) || (table == OP) || (table == DIR) || (table == MAQ) || (table == CAL) || (table == X_CAL))
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        } while (car != '\t');
    }
    else
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        } while (car != '\n');
    }
    fseek(hc12, pos, SEEK_SET);
    c = (char *)calloc((lon + 1), sizeof(char));
    if (fgets(c, lon + 1, hc12) == 0)
        printf("fgets() failed\n");
    printf("<<%s>>\n", c);
    delete(c);
    return c;
}

void delete(char *c)
{
    char *ptr;
    if (((ptr = strchr(c, '\n')) != NULL) || ((ptr = strchr(c, '\t')) != NULL) || ((ptr = strchr(c, ' ')) != NULL))
        *ptr = '\0';
}

输出:

Non-tab: <<ADCA >>
ADCA    <<SI    >>
SI  <<IMM   >>
IMM <<89ii  >>
89ii    <<1 >>
1   <<1 >>
1   <<2
>>
2
Tab:     <<EXT  >>
        EXT <<B9hhll    >>
B9hhll  <<1 >>
1   <<2 >>
2   <<3
>>
3
Tab:     <<IDX1 >>
        IDX1    <<A9xbff    >>
A9xbff  <<1 >>
1   <<2 >>
2   <<3
>>
3
Tab:     <<[D,IDX]  >>
        [D,IDX] <<A9xb  >>
A9xb    <<1 >>
1   <<1 >>
1   <<2
>>
2

或多或少固定版本的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 8

typedef enum { INS, OP, DIR, MAQ, CAL, X_CAL, TOTAL } table;

void delete(char *c);
void SearchEndLine(FILE *fd);
void ignoreSpaces(FILE *fd);
char *Operands(FILE *hc12, int table);

int main(void)
{
    int car, i;
    FILE *hc12;
    char *ins, *op, *dir[MAX], *maq[MAX], *cal[MAX], *x_cal[MAX], *s[MAX];
    if ((hc12 = fopen("file.txt", "r")) != NULL)
    {
        i = 0;
        while ((car = fgetc(hc12)) != EOF)
        {
            if (car != '\t')
            {
                printf("Non-tab: ");
                ins = Operands(hc12, INS);
                printf("%s\t", ins);
                ignoreSpaces(hc12);
                op = Operands(hc12, OP);
                printf("%s\t", op);
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12, DIR);
                printf("%s\t", dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12, MAQ);
                printf("%s\t", maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12, CAL);
                printf("%s\t", cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12, X_CAL);
                printf("%s\t", x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12, TOTAL);
                printf("%s\n", s[i]);
                SearchEndLine(hc12);
            }
            else
            {
                printf("Tab:     ");
                ignoreSpaces(hc12);
                dir[i] = Operands(hc12, DIR);
                printf("\t\t%s\t", dir[i]);
                ignoreSpaces(hc12);
                maq[i] = Operands(hc12, MAQ);
                printf("%s\t", maq[i]);
                ignoreSpaces(hc12);
                cal[i] = Operands(hc12, CAL);
                printf("%s\t", cal[i]);
                ignoreSpaces(hc12);
                x_cal[i] = Operands(hc12, X_CAL);
                printf("%s\t", x_cal[i]);
                ignoreSpaces(hc12);
                s[i] = Operands(hc12, TOTAL);
                printf("%s\n", s[i]);
                SearchEndLine(hc12);
            }
            i++;
        }
    }
    return 0;
}

void SearchEndLine(FILE *fd)
{
    int car;
    while ((car = fgetc(fd)) != '\n')
        ;
}

void ignoreSpaces(FILE *fd)
{
    int car;
    do
    {
        car = fgetc(fd);
    } while (car == '\t' || car == ' ');
}

char *Operands(FILE *hc12, int table)
{
    int car, lon = 0, pos;
    char *c;
    fseek(hc12, -1, SEEK_CUR);
    pos = ftell(hc12);
    if ((table == INS) || (table == OP) || (table == DIR) || (table == MAQ) || (table == CAL) || (table == X_CAL))
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        } while (car != '\t' && car != EOF);
    }
    else
    {
        do
        {
            car = fgetc(hc12);
            lon++;
        } while (car != '\n' && car != EOF);
        lon--;
    }
    fseek(hc12, pos, SEEK_SET);
    c = (char *)calloc((lon + 1), sizeof(char));
    if (fgets(c, lon + 1, hc12) == 0)
        printf("fgets() failed\n");
    //printf("<<%s>>\n", c);
    delete(c);
    return c;
}

void delete(char *c)
{
    char *ptr;
    if (((ptr = strchr(c, '\n')) != NULL) || ((ptr = strchr(c, '\t')) != NULL) || ((ptr = strchr(c, ' ')) != NULL))
        *ptr = '\0';
}

输出:

Non-tab: ADCA   SI      IMM     89ii    1       1       2
Tab:                    DIR     99dd    1       1       2
Tab:                    EXT     B9hhll  1       2       3
Tab:                    IDX     A9xb    1       1       2
Tab:                    IDX1    A9xbff  1       2       3
Tab:                    IDX2    A9xbeeff        1       3       4
Tab:                    [D,IDX] A9xb    1       1       2
Tab:                    [IDX2]  A9xbeeff        1       3       4

仍有很大的改进空间。