How to write pointers inside a binary file? (C)
up vote
0
down vote
favorite
I am not very used to work with a file, but when is necessary to write strings or normal variables, I don't find any problem to put them inside a binary file. However, I have tried several times to save the content of a pointer to pointer (actually, an array of strings) inside a b-file and all the time or my compiler showed an error or just crashed. Does someone have an idea of how to make it work?
A simplified version of the code is down there:
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
campo2[i] = (char*)malloc(20*sizeof(char));
for(int i = 0; i< n; i++)
campo2[i] = foo();
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20*sizeof(char),1,binaryFile);
return 0;
c pointers binaryfiles
add a comment |
up vote
0
down vote
favorite
I am not very used to work with a file, but when is necessary to write strings or normal variables, I don't find any problem to put them inside a binary file. However, I have tried several times to save the content of a pointer to pointer (actually, an array of strings) inside a b-file and all the time or my compiler showed an error or just crashed. Does someone have an idea of how to make it work?
A simplified version of the code is down there:
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
campo2[i] = (char*)malloc(20*sizeof(char));
for(int i = 0; i< n; i++)
campo2[i] = foo();
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20*sizeof(char),1,binaryFile);
return 0;
c pointers binaryfiles
3
campo2[i] =:inot yet declared. Did you try to compile this code???
– Paul Ogilvie
Nov 10 at 13:43
Beforereturnyou mustfclosethe file.
– Paul Ogilvie
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Are you trying to write the strings pointed to bycampos2or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?
– Hogstrom
Nov 10 at 16:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am not very used to work with a file, but when is necessary to write strings or normal variables, I don't find any problem to put them inside a binary file. However, I have tried several times to save the content of a pointer to pointer (actually, an array of strings) inside a b-file and all the time or my compiler showed an error or just crashed. Does someone have an idea of how to make it work?
A simplified version of the code is down there:
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
campo2[i] = (char*)malloc(20*sizeof(char));
for(int i = 0; i< n; i++)
campo2[i] = foo();
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20*sizeof(char),1,binaryFile);
return 0;
c pointers binaryfiles
I am not very used to work with a file, but when is necessary to write strings or normal variables, I don't find any problem to put them inside a binary file. However, I have tried several times to save the content of a pointer to pointer (actually, an array of strings) inside a b-file and all the time or my compiler showed an error or just crashed. Does someone have an idea of how to make it work?
A simplified version of the code is down there:
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
campo2[i] = (char*)malloc(20*sizeof(char));
for(int i = 0; i< n; i++)
campo2[i] = foo();
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20*sizeof(char),1,binaryFile);
return 0;
c pointers binaryfiles
c pointers binaryfiles
edited Nov 10 at 18:18
asked Nov 10 at 13:39
Joao Marco Barros
11
11
3
campo2[i] =:inot yet declared. Did you try to compile this code???
– Paul Ogilvie
Nov 10 at 13:43
Beforereturnyou mustfclosethe file.
– Paul Ogilvie
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Are you trying to write the strings pointed to bycampos2or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?
– Hogstrom
Nov 10 at 16:13
add a comment |
3
campo2[i] =:inot yet declared. Did you try to compile this code???
– Paul Ogilvie
Nov 10 at 13:43
Beforereturnyou mustfclosethe file.
– Paul Ogilvie
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Are you trying to write the strings pointed to bycampos2or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?
– Hogstrom
Nov 10 at 16:13
3
3
campo2[i] =: i not yet declared. Did you try to compile this code???– Paul Ogilvie
Nov 10 at 13:43
campo2[i] =: i not yet declared. Did you try to compile this code???– Paul Ogilvie
Nov 10 at 13:43
Before
return you must fclose the file.– Paul Ogilvie
Nov 10 at 13:45
Before
return you must fclose the file.– Paul Ogilvie
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Are you trying to write the strings pointed to by
campos2 or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?– Hogstrom
Nov 10 at 16:13
Are you trying to write the strings pointed to by
campos2 or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?– Hogstrom
Nov 10 at 16:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You probably want something like
char * foo(int i)
char *s= malloc(20);
fprintf(s,"%018drn",i);
return s;
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
for(int i = 0; i< n; i++)
campo2[i] = foo(i);
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20,1,binaryFile);
fclose(binaryFile);
return 0;
1
Maybe you intendedsprintf(s,"%018drn",i);? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You probably want something like
char * foo(int i)
char *s= malloc(20);
fprintf(s,"%018drn",i);
return s;
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
for(int i = 0; i< n; i++)
campo2[i] = foo(i);
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20,1,binaryFile);
fclose(binaryFile);
return 0;
1
Maybe you intendedsprintf(s,"%018drn",i);? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41
add a comment |
up vote
0
down vote
You probably want something like
char * foo(int i)
char *s= malloc(20);
fprintf(s,"%018drn",i);
return s;
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
for(int i = 0; i< n; i++)
campo2[i] = foo(i);
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20,1,binaryFile);
fclose(binaryFile);
return 0;
1
Maybe you intendedsprintf(s,"%018drn",i);? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41
add a comment |
up vote
0
down vote
up vote
0
down vote
You probably want something like
char * foo(int i)
char *s= malloc(20);
fprintf(s,"%018drn",i);
return s;
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
for(int i = 0; i< n; i++)
campo2[i] = foo(i);
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20,1,binaryFile);
fclose(binaryFile);
return 0;
You probably want something like
char * foo(int i)
char *s= malloc(20);
fprintf(s,"%018drn",i);
return s;
int main()
int n;
scanf("%d", &n);
char **campo2 = (char**)malloc(n*sizeof(char*));
for(int i = 0; i< n; i++)
campo2[i] = foo(i);
FILE *binaryFile = fopen("file.bin", "wb");
if (binaryFile =! NULL)
for(int i = 0;i<n;i++)
fwrite(campo2[i], 20,1,binaryFile);
fclose(binaryFile);
return 0;
answered Nov 10 at 13:50
Paul Ogilvie
16.8k11134
16.8k11134
1
Maybe you intendedsprintf(s,"%018drn",i);? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41
add a comment |
1
Maybe you intendedsprintf(s,"%018drn",i);? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41
1
1
Maybe you intended
sprintf(s,"%018drn",i); ? (plus: 18 is too large here)– wildplasser
Nov 10 at 14:41
Maybe you intended
sprintf(s,"%018drn",i); ? (plus: 18 is too large here)– wildplasser
Nov 10 at 14:41
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53239535%2fhow-to-write-pointers-inside-a-binary-file-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
3
campo2[i] =:inot yet declared. Did you try to compile this code???– Paul Ogilvie
Nov 10 at 13:43
Before
returnyou mustfclosethe file.– Paul Ogilvie
Nov 10 at 13:45
Ignoring the various typos and errors, you’re allocating 20 chars for your data, but trying to write 30. I suspect that’s part of your problem.
– John Bode
Nov 10 at 13:45
Why do you want to write pointers to a file? What would that be good for?
– melpomene
Nov 10 at 13:46
Are you trying to write the strings pointed to by
campos2or the pointers? I assume the strings as the value of the pointers doesn't make sense to me. Also, can you provide a complete example including foo ? Including what the input value is and what your actually expecting in the file ?– Hogstrom
Nov 10 at 16:13