Why do I have some trouble with fwrite() and fread()?

Denis_newbie

This is my program:

#include <iostream>

int main()
{
    // open file1.txt
    FILE* file1;
    fopen_s(&file1, "file1.txt", "wb+");

    // write array of 5 integers to file.txt
    int array1[5] { 1, 2, 3, 4, 5 };
    for (int i = 0; i < 5; i++)
    {
        fwrite(array1, sizeof(array1[0]), 5, file1);
    }

    fseek(file1, 0, SEEK_SET);
    int tempValue;
    fread(&tempValue, sizeof(tempValue), 1, file1);
    // fseek(file1, 0, SEEK_CUR);
    fwrite(&tempValue, sizeof(tempValue), 1, file1);
}

At runtime the program crashed with informatoin:

>     Expression ("Flush between consecutive read and write.",                 
>     !stream.has_any_of(_IOREAD))

But if I uncomment fseek(file1, 0, SEEK_CUR); everything would be fine considering file pointer has not been moved. So why is that? I use Visual Studio 2019


P.S. Why this works just fine?

#include <iostream>

int main()
{
    FILE* file1;
    fopen_s(&file1, "data.txt", "wb+");
    int value = 7;
    fwrite(&value, sizeof(value), 1, file1);
    fwrite(&value, sizeof(value), 1, file1);

    fseek(file1, 0, SEEK_CUR);
    fwrite(&value, sizeof(value), 1, file1);
    fread(&value, sizeof(value), 1, file1);
}
Ace Figueroa

Read to write

Pulled from Microsofts C Runtime Library documentation

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed. (The file is said to be open for "update".) However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file-positioning function. The file-positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file-positioning function.

Changing between read/write operations require a file-position function, In your code snippet, you have:

...
fseek(file1, 0, SEEK_SET);
int tempValue;
fread(&tempValue, sizeof(tempValue), 1, file1);
// fseek(file1, 0, SEEK_CUR);
fwrite(&tempValue, sizeof(tempValue), 1, file1);
...

Since you are changing from read to write, you need to call a file-position function (fsetpos, fseek or rewind).


Write to Read

As for write to read, you will still need to call a file-position function. However to answer why the second code block works, we need to know what fwrite() does on success.

According to the same Microsoft documentation,

... The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written.

Consider the code that you have provided:

...
FILE* file1;
fopen_s(&file1, "data.txt", "wb+");
int value = 7;
fwrite(&value, sizeof(value), 1, file1);
fwrite(&value, sizeof(value), 1, file1);

fseek(file1, 0, SEEK_CUR);
fwrite(&value, sizeof(value), 1, file1);
fread(&value, sizeof(value), 1, file1);
...

Assuming all fwrite()'s succeed, your file pointer would be at the EOF. Since the input operation encounters EOF, the code block would execute just fine.

However, you should follow guidelines and call fsetpos, fseek or rewind should fwrite fail.


Similar Stackoverflow questions

C standard library corner case

Why is fseek or fflush always required between reading and writing in the update modes?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I have some trouble with fwrite() and fread()

From Dev

fopen fread and fwrite trouble

From Dev

Why do fwrite / fread not like 56.806201550387598?

From Dev

How to fwrite and fread endianness independant integers, such that I can fwrite and fread on many machines and always have the same result

From Dev

Do operations such as fwrite and fread have undefined behavior when stream is null?

From Dev

How do I fwrite and fread array of strings to file?

From Dev

How do the fwrite() and fread() work when i open a binary file

From Dev

I have some trouble with object in kotlin

From Dev

Vim, why do I have some parts highlighted?

From Dev

Why do some classes have an "I" in front of their name?

From Dev

fwrite and fread with malloced nested struct does not have expected behaviour

From Dev

I have some trouble in adding cell into UITableView with storyboard

From Dev

I have some trouble with calling state from redux with react

From Dev

I have trouble with some pointers, access location failed at the end of debugging

From Dev

Promises, I have some trouble getting the return of another function

From Dev

Having some trouble with a guessing game I have for an assignment in Java

From Dev

Is it possible for fread() or fwrite() to return a value greater than the number of items? Why?

From Dev

How do fread and fwrite distinguish between different data (types) in C?

From Dev

Why do i have to restart the Django server every time I make some changes in views?

From Dev

Why do some primitives have byte-codes and some do not?

From Dev

Why do some laptops not have a GPU slot?

From Dev

Why do some routers not have a WAN port?

From Dev

Why do some of functions have pass

From Dev

Why I have to return Some in unapply method

From Dev

I have trouble working with pyaudio . How do I fix the errors?

From Dev

Using fread and fwrite

From Dev

Issue with fwrite and fread in sequence

From Dev

fwrite and fread empty strings

From Dev

fread() and fwrite() using strings

Related Related

  1. 1

    I have some trouble with fwrite() and fread()

  2. 2

    fopen fread and fwrite trouble

  3. 3

    Why do fwrite / fread not like 56.806201550387598?

  4. 4

    How to fwrite and fread endianness independant integers, such that I can fwrite and fread on many machines and always have the same result

  5. 5

    Do operations such as fwrite and fread have undefined behavior when stream is null?

  6. 6

    How do I fwrite and fread array of strings to file?

  7. 7

    How do the fwrite() and fread() work when i open a binary file

  8. 8

    I have some trouble with object in kotlin

  9. 9

    Vim, why do I have some parts highlighted?

  10. 10

    Why do some classes have an "I" in front of their name?

  11. 11

    fwrite and fread with malloced nested struct does not have expected behaviour

  12. 12

    I have some trouble in adding cell into UITableView with storyboard

  13. 13

    I have some trouble with calling state from redux with react

  14. 14

    I have trouble with some pointers, access location failed at the end of debugging

  15. 15

    Promises, I have some trouble getting the return of another function

  16. 16

    Having some trouble with a guessing game I have for an assignment in Java

  17. 17

    Is it possible for fread() or fwrite() to return a value greater than the number of items? Why?

  18. 18

    How do fread and fwrite distinguish between different data (types) in C?

  19. 19

    Why do i have to restart the Django server every time I make some changes in views?

  20. 20

    Why do some primitives have byte-codes and some do not?

  21. 21

    Why do some laptops not have a GPU slot?

  22. 22

    Why do some routers not have a WAN port?

  23. 23

    Why do some of functions have pass

  24. 24

    Why I have to return Some in unapply method

  25. 25

    I have trouble working with pyaudio . How do I fix the errors?

  26. 26

    Using fread and fwrite

  27. 27

    Issue with fwrite and fread in sequence

  28. 28

    fwrite and fread empty strings

  29. 29

    fread() and fwrite() using strings

HotTag

Archive