C语言 如何使用 write 系统调用将 int 写入文件并完全按照写入的方式读取它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15746983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write int to file using write system call and read them exactly as written?
提问by Sam
How can I write int, float or other types to a file using the write system call of UNIX?
I want to do so without using any lib function like fprintfor fwrite.
如何使用 UNIX 的 write 系统调用将 int、float 或其他类型写入文件?我想这样做而不使用任何 lib 函数,如fprintf或fwrite。
I want to use file descriptor and not the FILE*.
我想使用文件描述符而不是FILE*.
After opening again, the file must be read exactly as written, without needing to know what size to read.
再次打开后,必须完全按照写入的方式读取文件,而无需知道要读取的大小。
回答by Jengerer
This is as simple as it can get (note that stdio.his only included for printf; the read/write works without it):
这很简单(请注意,stdio.h仅包含 for printf;没有它,读/写工作):
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
// Open file with write permission (create if doesn't exist).
int fd = open("float.txt", O_CREAT | O_WRONLY);
float val = 1.5f;
if (fd != -1) {
write(fd, &val, sizeof(val));
close(fd);
}
// Test read.
fd = open("float.txt", O_RDONLY);
float new_val;
if (fd != -1) {
read(fd, &new_val, sizeof(new_val));
printf("new_val = %f\n", new_val);
close(fd);
}
return 0;
}
回答by Barmar
int foo;
write (fd, &foo, sizeof foo);
回答by Eric Postpischil
As ascertained in comments, the problem is more to convert a number to a decimal numeral than to use the writecall.
正如评论中所确定的,问题更多的是将数字转换为十进制数字而不是使用write调用。
To write an int or float that is readable by humans, you must convert it to a numeral, then write the characters of that numeral. For float, if the internal representation uses a different base (e.g., binary) than the numeral (e.g., decimal), then this requires a lot of work to do correctly. I would recommend either using existing open-source code or a scientific paper on the topic.
要编写人类可读的 int 或 float,您必须将其转换为数字,然后写入该数字的字符。对于浮点数,如果内部表示使用与数字(例如十进制)不同的基数(例如,二进制),那么这需要大量工作才能正确执行。我建议使用现有的开源代码或关于该主题的科学论文。
The code to convert an intto a decimal numeral is fairly straightforward:
将 an 转换int为十进制数字的代码相当简单:
#include <stdlib.h> // For size_t.
/* IntToString converts the int x to a decimal numeral, which is written to s.
No terminal null character is written. The number of characters written is
returned. s MUST point to space with enough room for the numeral,
including a leading '-' if x is negative.
*/
size_t IntToString(char *s, int x)
{
// Set pointer to current position.
char *p = s;
// Set t to absolute value of x.
unsigned t = x;
if (x < 0) t = -t;
// Write digits.
do
{
*p++ = '0' + t % 10;
t /= 10;
} while (t);
// If x is negative, write sign.
if (x < 0)
*p++ = '-';
// Remember the return value, the number of characters written.
size_t r = p-s;
// Since we wrote the characters in reverse order, reverse them.
while (s < --p)
{
char t = *s;
*s++ = *p;
*p = t;
}
return r;
}
#include <stdio.h> // For printf.
// Demonstrate IntToString.
static void Demonstrate(int x)
{
char buf[100];
size_t n = IntToString(buf, x);
printf("IntToString(%d) = %.*s.\n", x, (int) n, buf);
}
#include <limits.h> // For INT_MIN and INT_MAX.
int main(void)
{
Demonstrate(0);
Demonstrate(1);
Demonstrate(9);
Demonstrate(10);
Demonstrate(INT_MAX-1);
Demonstrate(INT_MAX);
Demonstrate(-1);
Demonstrate(-9);
Demonstrate(-10);
Demonstrate(INT_MIN+1);
Demonstrate(INT_MIN);
return 0;
}

