如何INT写入文件使用write系统调用,准确地读出他们作为写的? [英] How to write int to file using write system call and read them exactly as written?

查看:311
本文介绍了如何INT写入文件使用write系统调用,准确地读出他们作为写的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写整数,浮点或其他类型的使用UNIX的write系统调用的文件?
我不想使用像任何LIB函数fprintf中这样做 FWRITE

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 fprintf or fwrite.

我想用文件描述符,而不是 FILE *

I want to use file descriptor and not the FILE*.

再次打开后,文件必须完全读出书面,而不需要知道读什么书的大小。

After opening again, the file must be read exactly as written, without needing to know what size to read.

推荐答案

作为意见确定的,这个问题更将数字转换为十进制数字,而不是使用调用。

As ascertained in comments, the problem is more to convert a number to a decimal numeral than to use the write call.

要编写一个整数或浮点数是人类可读的,则必须将其转换为数字,然后写数字的字符。对于浮动,如果内部再presentation使用不同的碱(如二进制)比数字(例如,小数),那么这需要大量的工作做正确。我会建议或者使用现有的开放源代码code或在主题的科学论文。

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.

在code,将一个 INT 十进制数字是相当简单:

The code to convert an int to a decimal numeral is fairly straightforward:

#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;
}

这篇关于如何INT写入文件使用write系统调用,准确地读出他们作为写的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆