简体   繁体   中英

How do you write the same bytes twice in C?

short i = 1;
fwrite(&i, sizeof(i), 1, file);
fwrite(&i, sizeof(i), 1, file);

Is that proper? I want to write the same i twice.

There's nothing wrong with it; just remember to check the return value.

Yes, that will work fine. Alternatively you could declare an array of two and use a single fwrite , which would theoretically be more efficient:

short i[2] = {1, 1};
fwrite(i, sizeof(short), 2, file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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