Issue with fwrite and fread in sequence
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;
typedef struct TestStruct {
int data[30];
} TestStruct;
TestStruct *test = malloc(sizeof(TestStruct));
for (int i = 0; i < 30; i++)
{
test->data[i] = i;
}
const char *filename = "some.txt";
FILE *file = fopen(filename, "w+");
fwrite(test, sizeof(TestStruct), 1, file);
rewind(file);
TestStruct *test2 = malloc(sizeof(TestStruct));
int rc = fread(test2, sizeof(TestStruct), 1, file);
The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?
c windows io
add a comment |
I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;
typedef struct TestStruct {
int data[30];
} TestStruct;
TestStruct *test = malloc(sizeof(TestStruct));
for (int i = 0; i < 30; i++)
{
test->data[i] = i;
}
const char *filename = "some.txt";
FILE *file = fopen(filename, "w+");
fwrite(test, sizeof(TestStruct), 1, file);
rewind(file);
TestStruct *test2 = malloc(sizeof(TestStruct));
int rc = fread(test2, sizeof(TestStruct), 1, file);
The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?
c windows io
add a comment |
I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;
typedef struct TestStruct {
int data[30];
} TestStruct;
TestStruct *test = malloc(sizeof(TestStruct));
for (int i = 0; i < 30; i++)
{
test->data[i] = i;
}
const char *filename = "some.txt";
FILE *file = fopen(filename, "w+");
fwrite(test, sizeof(TestStruct), 1, file);
rewind(file);
TestStruct *test2 = malloc(sizeof(TestStruct));
int rc = fread(test2, sizeof(TestStruct), 1, file);
The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?
c windows io
I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;
typedef struct TestStruct {
int data[30];
} TestStruct;
TestStruct *test = malloc(sizeof(TestStruct));
for (int i = 0; i < 30; i++)
{
test->data[i] = i;
}
const char *filename = "some.txt";
FILE *file = fopen(filename, "w+");
fwrite(test, sizeof(TestStruct), 1, file);
rewind(file);
TestStruct *test2 = malloc(sizeof(TestStruct));
int rc = fread(test2, sizeof(TestStruct), 1, file);
The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?
c windows io
c windows io
edited Jan 4 at 2:54
Jonathan Leffler
574k956881041
574k956881041
asked Jan 4 at 2:34
Argus KosArgus Kos
628
628
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- You're working on a Windows or DOS platform.
- You're using binary data.
- You write control-Z to the file (byte code 26 at index 26).
- Because you didn't specify
b(for binary) in the mode string used withfopen(), the control-Z is treated as an EOF marker when you read the data.
Fix: Use "w+b" instead of just "w+" to deal with the problem.
Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.
You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.
1
Note also that the CRT does newline translation in text mode. Writing"n"gets converted to"rn"in the file, and reading"rn"gets converted back to"n".
– eryksun
Jan 4 at 4:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032463%2fissue-with-fwrite-and-fread-in-sequence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- You're working on a Windows or DOS platform.
- You're using binary data.
- You write control-Z to the file (byte code 26 at index 26).
- Because you didn't specify
b(for binary) in the mode string used withfopen(), the control-Z is treated as an EOF marker when you read the data.
Fix: Use "w+b" instead of just "w+" to deal with the problem.
Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.
You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.
1
Note also that the CRT does newline translation in text mode. Writing"n"gets converted to"rn"in the file, and reading"rn"gets converted back to"n".
– eryksun
Jan 4 at 4:05
add a comment |
- You're working on a Windows or DOS platform.
- You're using binary data.
- You write control-Z to the file (byte code 26 at index 26).
- Because you didn't specify
b(for binary) in the mode string used withfopen(), the control-Z is treated as an EOF marker when you read the data.
Fix: Use "w+b" instead of just "w+" to deal with the problem.
Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.
You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.
1
Note also that the CRT does newline translation in text mode. Writing"n"gets converted to"rn"in the file, and reading"rn"gets converted back to"n".
– eryksun
Jan 4 at 4:05
add a comment |
- You're working on a Windows or DOS platform.
- You're using binary data.
- You write control-Z to the file (byte code 26 at index 26).
- Because you didn't specify
b(for binary) in the mode string used withfopen(), the control-Z is treated as an EOF marker when you read the data.
Fix: Use "w+b" instead of just "w+" to deal with the problem.
Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.
You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.
- You're working on a Windows or DOS platform.
- You're using binary data.
- You write control-Z to the file (byte code 26 at index 26).
- Because you didn't specify
b(for binary) in the mode string used withfopen(), the control-Z is treated as an EOF marker when you read the data.
Fix: Use "w+b" instead of just "w+" to deal with the problem.
Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.
You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.
answered Jan 4 at 2:45
Jonathan LefflerJonathan Leffler
574k956881041
574k956881041
1
Note also that the CRT does newline translation in text mode. Writing"n"gets converted to"rn"in the file, and reading"rn"gets converted back to"n".
– eryksun
Jan 4 at 4:05
add a comment |
1
Note also that the CRT does newline translation in text mode. Writing"n"gets converted to"rn"in the file, and reading"rn"gets converted back to"n".
– eryksun
Jan 4 at 4:05
1
1
Note also that the CRT does newline translation in text mode. Writing
"n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".– eryksun
Jan 4 at 4:05
Note also that the CRT does newline translation in text mode. Writing
"n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".– eryksun
Jan 4 at 4:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032463%2fissue-with-fwrite-and-fread-in-sequence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown