【C++】fprinttf, fwrite, fstream::write

最近在做文件IO读写相关的工作,以前一直对printf、fwrite和c++的write不是特别熟悉,写个随笔记录下。

fprintf

int fprintf( FILE *stream, const char *format, ... );

Loads the data from the given locations, converts them to character string equivalents and writes the results to the output stream.

The format string consists of ordinary multibyte characters (except %), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format.

Return the number of characters transmitted to the output stream or negative value if an output error or an encoding error (for string and character conversion specifiers) occurred.

 

fwrite

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

Writes count of objects from the given array buffer to the output stream stream. The objects are written as if by reinterpreting each object as an array of unsigned char and calling fputc size times for each object to write those unsigned chars into stream, in order. The file position indicator for the stream is advanced by the number of characters written.

 

std::basic_ostream<CharT,Traits>::write

class basic_ofstream : public basic_ostream<_CharT,_Traits>{};
class basic_ifstream : public basic_istream<_CharT, _Traits>{};
class basic_iostream : public basic_istream<_CharT, _Traits>, public basic_ostream<_CharT, _Traits>{};
class basic_fstream : public basic_iostream<_CharT, _Traits>{};

Behaves as an UnformattedOutputFunction. After constructing and checking the sentry object, outputs the characters from successive locations in the character array whose first element is pointed to by s. Characters are inserted into the output sequence until one of the following occurs:

  • exactly count characters are inserted
  • inserting into the output sequence fails (in which case setstate(badbit) is called)

 

三者区别

fprintf输出的是格式化后的字符串,按每个字符的ascii码直接写入;

fwrite和write是输出二进制,本质上是按内存挨个写入文件流。

两个示例加深理解:

示例1:

#include <bits/stdc++.h>

int main()
{
    const char* buf = "123abc";

    FILE *f1 = fopen("testfprintf.txt", "w+");
    FILE *f2 = fopen("testfwrite.txt", "w+");
    std::ofstream f3("testwrite", std::ios::out);

    fprintf(f1, "%s", buf);
    fwrite(buf, sizeof(char), strlen(buf), f2);
    f3.write(buf, strlen(buf));

    fclose(f1);
    fclose(f2);
    f3.close();

    return 0;
}

上面程序运行后,三个文件的内容都是123abc。

fprintf写入的是 1(31), 2(32), 3(33), a(61), b(62), c(63);
fwrite和write写入的是"123abc"在内存中存的值,对应的ascii和上面fprintf相同,所以最后文件内容相同。

 

 

示例2:

#include <bits/stdc++.h>

int main()
{
    int buf[] = {3, 5};

    FILE *f1 = fopen("testfprintf.txt", "w+");
    FILE *f2 = fopen("testfwrite.txt", "w+");
    std::ofstream f3("testwrite", std::ios::out);

    fprintf(f1, "%d, %d", buf[0], buf[1]);
    fwrite(buf, sizeof(int), sizeof(buf) / sizeof(buf[0]), f2);
    f3.write(reinterpret_cast<char*>(buf), sizeof(buf));

    fclose(f1);
    fclose(f2);
    f3.close();

    return 0;
}

程序运行结果:

fprintf按照格式化输出3 和5 没有什么疑问。

fwrite按照字节输出,3在内存中占4个字节:00 00 00 03,所以输出到文件中也是00 00 00 03。 第二个数字也是同样的原理。

对于write,和fwrite原理相同,输出内容也相等。

 

posted @ 2021-05-16 21:51  风不会停息gcc  阅读(575)  评论(0)    收藏  举报