[英]How to create an int file
我正在嘗試創建一個共享文件,該文件必須為int。
int f;
但是,當我到達
if ((fstat(f, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode)))
它給了我一個錯誤。 該文件必須包含類似以下的字符串:
abcd
我的猜測如下,但它不起作用:
int main() {
int f;
void *memoria = NULL;
int tam;
struct stat stbuf;
char string[15] = {0};
f = open("fichero.txt", O_RDWR, S_IRUSR);
// ERROR IS HERE!!
if ((fstat(f, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) {
printf("Error");
}
tam = stbuf.st_size;
printf("%d\n", tam);
// Proyect the file
memoria = mmap(0, tam, PROT_WRITE, MAP_SHARED, f, 0);
// Copy the string into the file
memcpy(memoria, "abcd", 5);
munmap(memoria, tam);
return 0;
}
我應該在開放狀態下更改參數嗎? 我究竟做錯了什么? 謝謝!
如果文件不存在,則需要使用O_CREAT
模式創建文件。
f = open("fichero.txt", O_RDWR | O_CREAT, S_IRUSR);
您應該檢查open()
錯誤:
if (f == -1) {
perror("open");
exit(1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.