我有一個目錄的名稱,然後我使用chdir()來更改目錄。 現在,我怎樣才能獲得總字節數?如何獲取來自目錄中所有文件的字節總數C
由於
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>
#include<sys/wait.h>
#include<sys/types.h>
void print_cwd(){
char cwd[100];
memset(cwd,0,sizeof(cwd));
printf("current directory: %s \n",getcwd(cwd,sizeof(cwd)));
}
int main(){
int fd[2],nbytes;
pid_t childpid;
char string[]="hello world!\n";
char sir[80];
char readbuffer[80];
pipe(fd);
if ((childpid=fork())<0){
printf("error");
exit(1);
}
if (childpid==0){
close(fd[0]);
scanf("%s",sir);
write(fd[1],sir,(strlen(sir)+1));
exit(0);
}
else{
close(fd[1]);
nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
chdir(readbuffer);
print_cwd();
}
return(0);
}
你能提供你的問題的代碼示例嗎? – PrestonM
使用'readdir'獲取文件列表,然後使用'stat'獲取文件的大小。 – ikegami
我寫了。 @PrestonM – Scusf0