【发布时间】:2014-03-17 00:06:44
【问题描述】:
我正在从一个文件 emp 中读取,它正在读取文件(这是下面的最后一个文件——它工作)被构造为 10 个带有标题的记录(被 fseek() 跳过代码中的 0)。但是,当它以 3x10 格式(这是下面的中间文件,无法正确读取)读取每个 3 块的标题时——那就失败了。我不确定为什么循环中的条件没有捕获,循环中的第二个条件没有打印前面标记为 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 的任何内容,并跳过“标题”行在每个哈希块的前面。当我试图强制它打印时(比如 print
它应该做的是打印所有以0开头的记录并跳过标题(因为它们的开头没有-1,0,1)。
【问题讨论】:
标签: c file-io string-formatting scanf