我正在从一个文件 emp
中读取,当文件(这是下面的最后一个文件 - 有效)被构造为 10 strip 有标题的记录(被跳过代码中 0 的 fseek()
)。但是,当它以 3x10 格式(这是下面的中间文件,未能正确读取)读入时,每个 3 block 的标题都失败了。我不确定为什么循环内的条件没有被捕获,并且循环内的第二个条件没有打印前面标有 0 的所有内容。
int nDeleteSwitch;
char sSSN[10], sName[21];
float nSalary;
char nextIn[3];
printf("SSN NAME SALARY\n");
mioHashFile = fopen("emp", "r");
fscanf (mioHashFile,"%d",&mnOverFlowRecords);
fseek (mioHashFile, mnHeaderSize, 0); //Skip past the CRLF at end of overflow counter
//int numHeadRec = mnOverFlowRecords/3;
/* sequentially print all active records */
//for(int i=0;i<(numHeadRec+mnOverFlowRecords);i++)
for(int i=0;i<20+mnOverFlowRecords;i++)
{
if ((fscanf(mioHashFile,"%d",nextIn)== -1) || (fscanf(mioHashFile,"%d",nextIn)== 0) ||
(fscanf(mioHashFile,"%d",nextIn)== 1))
{
fscanf(mioHashFile,"%d%s%s%f",&nDeleteSwitch,sSSN,sName,&nSalary);
//printf("%d",nDeleteSwitch);
if (nDeleteSwitch==0)printf("%-11s%-21s%-10.2f\n",sSSN,sName,nSalary); // wtf why this isn't printing
else if (nDeleteSwitch == -1) printf("there's a -1 on row: %d",i);
}
else {continue;};
}
fclose(mioHashFile);
printf("Print Table Complete\n");
这里我们有一个 emp 文件,它拒绝从以下位置读取 0
条目:
0
Overflow page: 0 0 -1
-1 x x 0.00
-1 x x 0.00
0 x x 0.00
Overflow page: 1 0 -1
-1 x x 0.00
-1 x x 0.00
0 x x 0.00
Overflow page: 2 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 3 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 4 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 5 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 6 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 7 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 8 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
Overflow page: 9 0 -1
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
所以它不会读取那个,但它会读取这个:
0
0 123 asd 789.00
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
0 345 zxc 234.00
-1 x x 0.00
-1 x x 0.00
-1 x x 0.00
-1 之前有一个空格,0 之前有 2 个空格。如代码所示,我试图打印任何开头带有 0 的内容,并跳过“标题”行在每个哈希 block 的前面。当我试图强制它打印(如 print < 3)时,它只是作为垃圾符号出现。
它应该做的是打印所有开头有 0
的记录并跳过标题(因为它们没有 -1,0,1
在开头)。
最佳答案
鉴于声明:
char nextIn[3];
这段代码是错误的:
if ((fscanf(mioHashFile,"%d",nextIn) == -1) || (fscanf(mioHashFile,"%d",nextIn) == 0) ||
(fscanf(mioHashFile,"%d",nextIn) == 1))
{
您正在传递一个包含三个字符的数组,并期望 fscanf()
将其视为 int
。你的条件也很奇怪。不能保证 EOF 为 -1
(尽管我不记得不是这样的系统)。在检测到 EOF 后重试没有多大意义(不是没有其他事件来清除 EOF 标记)。
When I try this
fscanf(mioHashFile,"%s",nextIn); if (strcmp(nextIn, '-1') == 0)
it still crashes and burns opulently due to type incompatibility. @Emmet also caught my error on the EOF (in that answer's code). How would you do it, and still maintain thescanf()
,printf()
formatting that I'm trying to use?
这需要一些半真实的编码。我会使用 fgets()
和 sscanf()
而不是 fscanf()
:
char line[4096];
int lineno = 0;
while (fgets(line, sizeof(line), mioHashFile) != 0)
{
if (++lineno == 1)
continue; // Skip the offset line
if (line[0] == 'O') // Overflow line - skip
continue;
if (sscanf(line, "%d %9s %20s %f", &nDeleteSwitch, sSSN, sName, &nSalary) != 4)
{
fprintf(stderr, "Failed to scan data from: %s", line);
continue;
}
if (nDeleteSwitch == 0)
printf("%-11s%-21s%-10.2f\n", sSSN, sName, nSalary);
else if (nDeleteSwitch == -1)
printf("there's a -1 on row: %d\n", lineno);
else
printf("The delete switch value is %d\n", nDeleteSwitch);
}
请注意,%s
会跳过前导空白,然后在下一个空白处停止扫描。
关于C -- 文件输入读取问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444861/