gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:39:57 25 4
gpt4 key购买 nike

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

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';
}

最佳答案

问题出在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

仍有很大的改进空间。

关于c - 为什么这段代码不打印文件的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com