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;










share|improve this question



















  • 3




    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










  • 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 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














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;










share|improve this question



















  • 3




    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










  • 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 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












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;










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:18

























asked Nov 10 at 13:39









Joao Marco Barros

11




11







  • 3




    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










  • 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 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












  • 3




    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










  • 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 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







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












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;






share|improve this answer
















  • 1




    Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
    – wildplasser
    Nov 10 at 14:41










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',
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
);



);













draft saved

draft discarded


















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

























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;






share|improve this answer
















  • 1




    Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
    – wildplasser
    Nov 10 at 14:41














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;






share|improve this answer
















  • 1




    Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
    – wildplasser
    Nov 10 at 14:41












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;






share|improve this answer












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;







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 1




    Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
    – wildplasser
    Nov 10 at 14:41







1




1




Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41




Maybe you intendedsprintf(s,"%018drn",i); ? (plus: 18 is too large here)
– wildplasser
Nov 10 at 14:41

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus