Troubles with files in C
I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "intarr.h"
/* LAB 6 TASK 1 */
/*
Save the entire array ia into a file called 'filename' in a binary
file format that can be loaded by intarr_load_binary(). Returns
zero on success, or a non-zero error code on failure. Arrays of
length 0 should produce an output file containing an empty array.
*/
int intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );
if( f == NULL )
{
return 1;
}
if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}
if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}
fclose( f );
return 0;
}
/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save_binary(). Returns a pointer to a
newly-allocated intarr_t on success, or NULL on failure.
*/
intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}
FILE* f = fopen( "filename", "rb" );
if( f == NULL )
{
return NULL;
}
int len;
if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}
intarr_t* new_ia = intarr_create( len );
fread( new_ia->data, sizeof( int ), len, f );
fclose( f );
return new_ia;
}
Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)
c file
add a comment |
I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "intarr.h"
/* LAB 6 TASK 1 */
/*
Save the entire array ia into a file called 'filename' in a binary
file format that can be loaded by intarr_load_binary(). Returns
zero on success, or a non-zero error code on failure. Arrays of
length 0 should produce an output file containing an empty array.
*/
int intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );
if( f == NULL )
{
return 1;
}
if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}
if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}
fclose( f );
return 0;
}
/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save_binary(). Returns a pointer to a
newly-allocated intarr_t on success, or NULL on failure.
*/
intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}
FILE* f = fopen( "filename", "rb" );
if( f == NULL )
{
return NULL;
}
int len;
if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}
intarr_t* new_ia = intarr_create( len );
fread( new_ia->data, sizeof( int ), len, f );
fclose( f );
return new_ia;
}
Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)
c file
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
We can't tell what typeia->datais. We can't tell whatintarr_createdoes. This makes it hard to give a good answer.
– David Schwartz
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at thefreadcall.
– paddy
Nov 21 '18 at 1:15
add a comment |
I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "intarr.h"
/* LAB 6 TASK 1 */
/*
Save the entire array ia into a file called 'filename' in a binary
file format that can be loaded by intarr_load_binary(). Returns
zero on success, or a non-zero error code on failure. Arrays of
length 0 should produce an output file containing an empty array.
*/
int intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );
if( f == NULL )
{
return 1;
}
if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}
if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}
fclose( f );
return 0;
}
/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save_binary(). Returns a pointer to a
newly-allocated intarr_t on success, or NULL on failure.
*/
intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}
FILE* f = fopen( "filename", "rb" );
if( f == NULL )
{
return NULL;
}
int len;
if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}
intarr_t* new_ia = intarr_create( len );
fread( new_ia->data, sizeof( int ), len, f );
fclose( f );
return new_ia;
}
Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)
c file
I am trying to write functions to help me with saving and loading files... but when I try to save my array from the file it is not matching the original one that I loaded in to the file. Here is my code.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "intarr.h"
/* LAB 6 TASK 1 */
/*
Save the entire array ia into a file called 'filename' in a binary
file format that can be loaded by intarr_load_binary(). Returns
zero on success, or a non-zero error code on failure. Arrays of
length 0 should produce an output file containing an empty array.
*/
int intarr_save_binary( intarr_t* ia, const char* filename )
{
FILE* f = fopen( "filename", "wb" );
if( f == NULL )
{
return 1;
}
if( fwrite( &ia->len, sizeof( int ), 1, f ) != 1 )
{
return 1;
}
if( fwrite( &ia->data, sizeof( int ), ia->len, f ) != ia->len )
{
return 1;
}
fclose( f );
return 0;
}
/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save_binary(). Returns a pointer to a
newly-allocated intarr_t on success, or NULL on failure.
*/
intarr_t* intarr_load_binary( const char* filename )
{
if( filename == NULL )
{
return NULL;
}
FILE* f = fopen( "filename", "rb" );
if( f == NULL )
{
return NULL;
}
int len;
if( fread( &len, sizeof( int ), 1, f ) != 1 )
{
return NULL;
}
intarr_t* new_ia = intarr_create( len );
fread( new_ia->data, sizeof( int ), len, f );
fclose( f );
return new_ia;
}
Also just to be clear intarr_t ia is just a struct with ia->data (array) and ia->len (len of array)
c file
c file
edited Nov 21 '18 at 0:59
zaack7
asked Nov 21 '18 at 0:53
zaack7zaack7
63
63
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
We can't tell what typeia->datais. We can't tell whatintarr_createdoes. This makes it hard to give a good answer.
– David Schwartz
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at thefreadcall.
– paddy
Nov 21 '18 at 1:15
add a comment |
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
We can't tell what typeia->datais. We can't tell whatintarr_createdoes. This makes it hard to give a good answer.
– David Schwartz
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at thefreadcall.
– paddy
Nov 21 '18 at 1:15
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
We can't tell what type
ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.– David Schwartz
Nov 21 '18 at 1:15
We can't tell what type
ia->data is. We can't tell what intarr_create does. This makes it hard to give a good answer.– David Schwartz
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the
fread call.– paddy
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the
fread call.– paddy
Nov 21 '18 at 1:15
add a comment |
1 Answer
1
active
oldest
votes
In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:
fwrite( &ia->data, sizeof( int ), ia->len, f )
The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:
fwrite( ia->data, sizeof( int ), ia->len, f )
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%2f53403821%2ftroubles-with-files-in-c%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
In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:
fwrite( &ia->data, sizeof( int ), ia->len, f )
The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:
fwrite( ia->data, sizeof( int ), ia->len, f )
add a comment |
In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:
fwrite( &ia->data, sizeof( int ), ia->len, f )
The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:
fwrite( ia->data, sizeof( int ), ia->len, f )
add a comment |
In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:
fwrite( &ia->data, sizeof( int ), ia->len, f )
The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:
fwrite( ia->data, sizeof( int ), ia->len, f )
In this line here, you are writing the contents of the pointer, not the data it points to. If the length is sufficient, you'll potentially write other random data that follows it, subject to undefined behavior:
fwrite( &ia->data, sizeof( int ), ia->len, f )
The problem is that you added one extra level of redirection by taking the address of ia->data. It seems like you just had a copy-paste error or something. Remove the &:
fwrite( ia->data, sizeof( int ), ia->len, f )
answered Nov 21 '18 at 1:12
paddypaddy
43.1k53176
43.1k53176
add a comment |
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%2f53403821%2ftroubles-with-files-in-c%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
What data are you saving? What is wrong with the data that is loaded?
– Retired Ninja
Nov 21 '18 at 0:59
Q: Have you tried looking at the output file in a hex editor, or the input struct with a debugger? Q: Is there any chance the readers/writers might have different byte endianness?
– paulsm4
Nov 21 '18 at 1:02
We can't tell what type
ia->datais. We can't tell whatintarr_createdoes. This makes it hard to give a good answer.– David Schwartz
Nov 21 '18 at 1:15
Yes, it's not a very clear question, but you can infer a pretty good guess at the type by looking at the
freadcall.– paddy
Nov 21 '18 at 1:15