简体   繁体   English

读写未签名的char缓冲区到C中的文件?

[英]Read and write a buffer of unsigned char to a file in C?

The following code writes an array of unsigned char (defined as byte ) to a file: 以下代码将无符号字符数组(定义为byte )写入文件:

typedef unsigned char byte;
void ToFile(byte *buffer, size_t len)
{
    FILE *f = fopen("out.txt", "w");
    if (f == NULL)
    {
        fprintf(stderr, "Error opening file!\n");
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < len; i++)
    {
        fprintf(f, "%u", buffer[i]);
    }
    fclose(f);
}

How do I read the file back from out.txt into a buffer of byte ? 如何将文件从out.txt读回byte缓冲区? The goal is to iterate the buffer byte by byte . 我们的目标是循环缓冲区bytebyte Thanks. 谢谢。

How do I read the file back from out.txt into a buffer of byte? 如何将文件从out.txt读回字节缓冲区? The goal is to iterate the buffer byte by byte. 目标是逐字节迭代缓冲区。 Thanks. 谢谢。

Something similar to this should work for you. 与此类似的东西应该对您有用。 (Not debugged, doing this away from my compiler) (未调试,请远离我的编译器执行此操作)

void FromFile(byte *buffer, size_t len)
{

    FILE *fOut = fopen("out.txt", "rb");  
    int cOut;

    int i = 0;
    if (fOut == NULL)
    {
        fprintf(stderr, "Error opening file!\n");
        exit(EXIT_FAILURE);
    }
    cOut = fgetc(fOut);
    while(cOut != EOF)
    {
        buffer[i++] = cOut;  //iterate buffer byte by byte

        cOut = fgetc(fOut);   
    }

    fclose(fOut);
}

If you want to read it back, I wouldn't use %u to write it out. 如果您想读回去,我不会用%u来写出来。 %u is going to be variable width output, so a 1 takes one character, and a 12 takes two, etc. When you read it back and see 112 you don't know if that's three characters (1, 1, 2), or two (11, 2; or 1, 12) or just one (112). %u将是可变宽度输出,因此1 %u一个字符, 12 %u两个字符,依此类推。当您读回它并看到112您不知道这是否是三个字符(1、2、1),或两个(11,2;或1,12)或仅一个(112)。 If you need an ASCII file, you would use a fixed width output, such as %03u. 如果需要ASCII文件,则可以使用固定宽度的输出,例如%03u。 That way each byte is always 3 characters. 这样,每个字节始终为3个字符。 Then you could read in a byte at a time with fscanf("%03u", buffer[i]) . 然后,您可以使用fscanf("%03u", buffer[i])一次读取一个字节。

You could (and should) use fread() and fwrite() ( http://www.cplusplus.com/reference/cstdio/fread/ ) for transferring raw memory between FILE s and memory. 您可以(并且应该)使用fread()fwrite()http://www.cplusplus.com/reference/cstdio/fread/ )在FILE和内存之间传输原始内存。

To determine the size of the file (to advise fread() how many bytes it should read) use fseek(f, 0, SEEK_END) ( http://www.cplusplus.com/reference/cstdio/fseek/ ) to place the cursor to the end of the file and read its size with ftell(f) ( http://www.cplusplus.com/reference/cstdio/ftell/ ). 要确定文件的大小(建议fread()应该读取多少字节),请使用fseek(f, 0, SEEK_END)http://www.cplusplus.com/reference/cstdio/fseek/ )来放置文件将光标移到文件末尾,并使用ftell(f)http://www.cplusplus.com/reference/cstdio/ftell/ )读取其大小。 Don't forget to jump back to the beginning with fseek(f, 0, SEEK_SET) for the actual reading process. 不要忘记从fseek(f, 0, SEEK_SET)开始重新开始实际的阅读过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM