我试图在创建文件并向其写入数据后获取文件的大小.我得到的值似乎与实际文件大小不符.这是我的计划.请告诉我如何以Bits,Bytes,Kilobytes和Megabytes显示文件大小.据我说,文件大小应该是288位,36字节,0.03515626千字节和0.000034332兆字节.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define PERMS 0777
int main(int argc, char *argv[])
{
int createDescriptor;
int openDescriptor;
char fileName[15]="Filename1.txt";
umask(0000);
if ((openDescriptor = creat(fileName, PERMS )) == -1)
{
printf("Error creating %s", fileName);
exit(EXIT_FAILURE);
}
if(write(openDescriptor,"This will be output to testfile.txt\n",36 ) != 36)
{
write(2,"There was an error writing to testfile.txt\n",43);
return 1;
}
if((close(openDescriptor))==-1)
{
write(2, "Error closing file.\n", 19);
}
struct stat buf;
fstat(openDescriptor, &buf);
int size=buf.st_size;
printf("%d\n",size);
printf("%u\n",size);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该fstat()函数有一个返回码,检查它.
int r = fstat(openDescriptor, &buf);
if (r) {
fprintf(stderr, "error: fstat: %s\n", strerror(errno));
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
这将打印:
error: fstat: Bad file descriptor
是的......你关闭了文件描述符,它不再是文件描述符了.你必须fstat()在打电话之前close().
这非常脆弱,在任何情况下都不能推荐:
if (write(openDescriptor,"This will be output to testfile.txt\n",36 ) != 36)
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
const char *str = "This will be output to testfile.txt\n";
if (write(fd, str, strlen(str)) != strlen(str))
Run Code Online (Sandbox Code Playgroud)
它将编译为相同的机器代码,并且它显然是正确的(与原始代码相反,您必须计算字符串中的字符数以确定它是否正确).
更好的是,当您使用时stderr,只需使用标准<stdio.h>功能:
fprintf(stderr, "There was an error writing to %s: %s\n",
fileName, strerror(errno));
Run Code Online (Sandbox Code Playgroud)
定义时出现相同的错误fileName...
// You should never have to know how to count higher than 4 to figure
// out if code is correct...
char fileName[15]="Filename1.txt";
// Do this instead...
static const char fileName[] = "Filename1.txt";
Run Code Online (Sandbox Code Playgroud)
你实际上错误计算了这个时间,[15]应该是[14],但最好把它留给编译器.使编译器的工作更容易没有任何好处,因为编译器可能没有更好的事情要做.
$ cat teststr.c
#include <unistd.h>
void func(int openDescriptor) {
write(openDescriptor,"This will be output to testfile.txt\n",36 );
}
$ cat teststr2.c
#include <string.h>
#include <unistd.h>
void func(int openDescriptor) {
const char *str = "This will be output to testfile.txt\n";
write(openDescriptor, str, strlen(str));
}
$ cc -S -O2 teststr.c
$ cc -S -O2 teststr2.c
$ diff teststr.s teststr2.s
1c1
< .file "teststr.c"
---
> .file "teststr2.c"
是的.如图所示,调用strlen()实际上不会导致不同的机器代码.