我正在從標準輸入讀取文件名,並且該函數返回的是完全錯誤的內容。下面的代碼返回4294967296,而不是應該是7.我在linux上運行這樣的文件:從stdin查找文件的大小給出的數字不正確
echo「p3test.txt」| ./totalsize
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
//find the file size
off_t filesize(const char* fileName){
printf("%s", fileName);
struct stat st;
if(stat(fileName, &st) == 0)
printf("%zd", st.st_size);
return st.st_size;
fprintf(stderr, "Cannot determine size of %s: %s\n",
fileName, strerror(errno));
return -1;
}
int main (int argc, char *argv[])
{
char tmpstring[1024];
const char* fileName;
off_t size;
while (fgets(tmpstring, 1024, stdin))
{
fileName = tmpstring;
size = filesize(fileName);
}
}
您可能缺少一些花括號。 – 5gon12eder 2015-03-02 19:30:23
我不能看到它會在哪裏,它編譯正確,我的支架熒光筆說我的括號很好 – FullCombatBeard 2015-03-02 19:31:59
@FullCombatBeard'if(stat(fileName,&st)== 0)'後面的兩條花線沒有大括號看起來很奇怪。 – myaut 2015-03-02 19:33:53