【发布时间】:2021-08-13 19:17:40
【问题描述】:
我的代码有点问题。我基本上有一个文件,每一行都包含一笔财富。我应该从用户那里得到出生日期,并从那里给出他们的星座和财富。
我有一个包含所有财富的文件。这是我迄今为止的工作:
//gets position in the array filled with the size of bytes per line
int ok = 0;
i = 0;
j = d.zodiac;
while(ok == 0 && i < arrSize){
if(j == zodiacMarker[i]){
zodiacMarker[i] = -1;
ok = 1;
}
i++;
}
locationMark = i - 1;
printf("\nLocation Mark: %i", locationMark);
//get the size of the file to get to the specific line in the file
int finalSize = 0;
for(int i = 0; i < locationMark; i++){
finalSize += byteSize[i];
}
//goes to the position
fseek(fp, finalSize+2, SEEK_SET);
printf("\nFinal Size: %i\n", finalSize);
printf("Where now: %i\n", ftell(fp));
printf("Size: %i\n", byteSize[locationMark+1]);
printf("fgets part : %i\n", byteSize[locationMark]+1);
char arr[byteSize[locationMark+1]];
//gets the string in that line
fgets(arr, byteSize[locationMark]+1, fp);
printf("%s", arr);
获取每行大小的逻辑的另一件事。我想获取每行的大小并将它们添加到我想要在行中的特定位置。
int charCount( FILE *const fp ){
char c;
int count = 0;
for( ;; ){
c = fgetc(fp);
if( c == EOF || c == '\n' )
break;
++count;
}
return count;
}
这是我的输出:
Month:4
Day: 8
Aries:
Location Mark: 2
Final Size: 106
Where now: 108
Size: 33
fgets part : 61
Thinking you know something is a sure way to blind yourself.
Month:3
Day: 6
Pisces:
Location Mark: 1
Final Size: 64
Where now: 66
Size: 60
fgets part : 43
ssumption is the mother of all screw-ups.
如您所见,一切都变得很不稳定。我不知道为什么。请发送帮助,如果您对如何解决此问题有更好的建议,请分享。非常感谢!
【问题讨论】:
-
只需在循环中使用
fgets来计算/跳过行数。它非常快。 -
byteSize是什么,它被填满了吗? -
您还必须将文件作为二进制文件处理才能正常工作,所以
"rb" -
您显示的代码不完整。很难提供帮助。
-
while 行是什么?
printf调用中没有“while”一词,while循环中也没有printf调用。