after fwrite destination file is empty c language [closed]










-1














i am trying to open two files, put their content in an array and write array back to the file. However, after i use fwrite function the destination file is empty. Could anybody explain how to achive my goal?



data.txt file content:



1
2
3


i.txt file content:



3
4
5


here is the code:



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

int main(void)

FILE *fmain, *fnew, *fp;
int i = 0,f = 0, length = 150, arr[length], chararr[length], sizearr;
char line[130];
int error;
fmain = fopen("data.txt", "rw+");
fnew = fopen("i.txt", "rw");

while(fgets(line, sizeof line, fnew) != NULL)
arr[f] = atoi(line);
f++;

fclose(fnew);
// read data into array from data file

while(fgets(line, sizeof line, fmain) != NULL)
arr[f] = atoi(line);
f++;


fclose(fmain);

fp = fopen("data2.txt", "w");

fwrite(arr, sizeof(char), sizeof(arr), fp);


fclose(fp);

return 0;



when i manually open data2.txt after i run the program it is empty but i would like to see something like:



1
2
3
3
4
5









share|improve this question















closed as off-topic by Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub Nov 12 at 15:49


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.












  • You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
    – Antti Haapala
    Nov 12 at 6:26











  • I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
    – AlexS
    Nov 12 at 6:40










  • Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
    – Antti Haapala
    Nov 12 at 6:41











  • thank you for your comment. i edited my question accordingly
    – AlexS
    Nov 12 at 6:48






  • 1




    That's not yet complete: what is supposed to be the contents of data2.txt.
    – Antti Haapala
    Nov 12 at 6:56















-1














i am trying to open two files, put their content in an array and write array back to the file. However, after i use fwrite function the destination file is empty. Could anybody explain how to achive my goal?



data.txt file content:



1
2
3


i.txt file content:



3
4
5


here is the code:



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

int main(void)

FILE *fmain, *fnew, *fp;
int i = 0,f = 0, length = 150, arr[length], chararr[length], sizearr;
char line[130];
int error;
fmain = fopen("data.txt", "rw+");
fnew = fopen("i.txt", "rw");

while(fgets(line, sizeof line, fnew) != NULL)
arr[f] = atoi(line);
f++;

fclose(fnew);
// read data into array from data file

while(fgets(line, sizeof line, fmain) != NULL)
arr[f] = atoi(line);
f++;


fclose(fmain);

fp = fopen("data2.txt", "w");

fwrite(arr, sizeof(char), sizeof(arr), fp);


fclose(fp);

return 0;



when i manually open data2.txt after i run the program it is empty but i would like to see something like:



1
2
3
3
4
5









share|improve this question















closed as off-topic by Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub Nov 12 at 15:49


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.












  • You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
    – Antti Haapala
    Nov 12 at 6:26











  • I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
    – AlexS
    Nov 12 at 6:40










  • Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
    – Antti Haapala
    Nov 12 at 6:41











  • thank you for your comment. i edited my question accordingly
    – AlexS
    Nov 12 at 6:48






  • 1




    That's not yet complete: what is supposed to be the contents of data2.txt.
    – Antti Haapala
    Nov 12 at 6:56













-1












-1








-1







i am trying to open two files, put their content in an array and write array back to the file. However, after i use fwrite function the destination file is empty. Could anybody explain how to achive my goal?



data.txt file content:



1
2
3


i.txt file content:



3
4
5


here is the code:



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

int main(void)

FILE *fmain, *fnew, *fp;
int i = 0,f = 0, length = 150, arr[length], chararr[length], sizearr;
char line[130];
int error;
fmain = fopen("data.txt", "rw+");
fnew = fopen("i.txt", "rw");

while(fgets(line, sizeof line, fnew) != NULL)
arr[f] = atoi(line);
f++;

fclose(fnew);
// read data into array from data file

while(fgets(line, sizeof line, fmain) != NULL)
arr[f] = atoi(line);
f++;


fclose(fmain);

fp = fopen("data2.txt", "w");

fwrite(arr, sizeof(char), sizeof(arr), fp);


fclose(fp);

return 0;



when i manually open data2.txt after i run the program it is empty but i would like to see something like:



1
2
3
3
4
5









share|improve this question















i am trying to open two files, put their content in an array and write array back to the file. However, after i use fwrite function the destination file is empty. Could anybody explain how to achive my goal?



data.txt file content:



1
2
3


i.txt file content:



3
4
5


here is the code:



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

int main(void)

FILE *fmain, *fnew, *fp;
int i = 0,f = 0, length = 150, arr[length], chararr[length], sizearr;
char line[130];
int error;
fmain = fopen("data.txt", "rw+");
fnew = fopen("i.txt", "rw");

while(fgets(line, sizeof line, fnew) != NULL)
arr[f] = atoi(line);
f++;

fclose(fnew);
// read data into array from data file

while(fgets(line, sizeof line, fmain) != NULL)
arr[f] = atoi(line);
f++;


fclose(fmain);

fp = fopen("data2.txt", "w");

fwrite(arr, sizeof(char), sizeof(arr), fp);


fclose(fp);

return 0;



when i manually open data2.txt after i run the program it is empty but i would like to see something like:



1
2
3
3
4
5






c fwrite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:54

























asked Nov 12 at 6:14









AlexS

61




61




closed as off-topic by Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub Nov 12 at 15:49


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub Nov 12 at 15:49


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, sideshowbarker, gsamaras, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.











  • You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
    – Antti Haapala
    Nov 12 at 6:26











  • I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
    – AlexS
    Nov 12 at 6:40










  • Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
    – Antti Haapala
    Nov 12 at 6:41











  • thank you for your comment. i edited my question accordingly
    – AlexS
    Nov 12 at 6:48






  • 1




    That's not yet complete: what is supposed to be the contents of data2.txt.
    – Antti Haapala
    Nov 12 at 6:56
















  • You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
    – Antti Haapala
    Nov 12 at 6:26











  • I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
    – AlexS
    Nov 12 at 6:40










  • Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
    – Antti Haapala
    Nov 12 at 6:41











  • thank you for your comment. i edited my question accordingly
    – AlexS
    Nov 12 at 6:48






  • 1




    That's not yet complete: what is supposed to be the contents of data2.txt.
    – Antti Haapala
    Nov 12 at 6:56















You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
– Antti Haapala
Nov 12 at 6:26





You're writing it to data2.txt and it is a binary file of 300 or 600 bytes. What makes you to think it is empty? Why are you writing a binary file with extension .txt? Why are you using the "w" (text mode) to create a binary file?
– Antti Haapala
Nov 12 at 6:26













I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
– AlexS
Nov 12 at 6:40




I just want to write an array to a text file and that was the best i could come up with. Could you elaborate on how i can do it since my solution is wrong?
– AlexS
Nov 12 at 6:40












Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
– Antti Haapala
Nov 12 at 6:41





Then you should edit your question and provide input with data.txt having 1, i.txt having 2 and the resulting file having what in it? I.e. the Minimal, Complete, and Verifiable example. You never state the expected output. It does write a file. A binary file with content, yet you claim the file is empty.
– Antti Haapala
Nov 12 at 6:41













thank you for your comment. i edited my question accordingly
– AlexS
Nov 12 at 6:48




thank you for your comment. i edited my question accordingly
– AlexS
Nov 12 at 6:48




1




1




That's not yet complete: what is supposed to be the contents of data2.txt.
– Antti Haapala
Nov 12 at 6:56




That's not yet complete: what is supposed to be the contents of data2.txt.
– Antti Haapala
Nov 12 at 6:56












2 Answers
2






active

oldest

votes


















1














  1. There is no "rw+" mode for fopen, you probably want the "r+" mode.

  2. You need to check if fopen was successful.

Try this:



 ...
fmain = fopen("data.txt", "r+");
if (fmain == NULL)

printf("Can'topen file.n"); exit(1);

fnew = fopen("i.txt", "r+");
if (fnew == NULL)

printf("Can'topen file.n"); exit(1);

...





share|improve this answer






















  • Thank you for but the answer but that was not the problem. I fixed it and it still not working
    – AlexS
    Nov 12 at 15:08


















0














You have to first check if the file you opened is not returning NULL and then only you can write to the file. As in here, the size of the data is fixed, write the entire array into a file is using the binary writing mode:



fp = fopen("data2.txt", "wb");
if(fp == NULL)

printf("Error!");
exit(1);

fwrite(arr, sizeof(char), sizeof(arr), fp);
fclose(fp);





share|improve this answer




















  • thank you for the answer. however, the data2.txt is still empty after i run the program
    – AlexS
    Nov 12 at 6:42

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














  1. There is no "rw+" mode for fopen, you probably want the "r+" mode.

  2. You need to check if fopen was successful.

Try this:



 ...
fmain = fopen("data.txt", "r+");
if (fmain == NULL)

printf("Can'topen file.n"); exit(1);

fnew = fopen("i.txt", "r+");
if (fnew == NULL)

printf("Can'topen file.n"); exit(1);

...





share|improve this answer






















  • Thank you for but the answer but that was not the problem. I fixed it and it still not working
    – AlexS
    Nov 12 at 15:08















1














  1. There is no "rw+" mode for fopen, you probably want the "r+" mode.

  2. You need to check if fopen was successful.

Try this:



 ...
fmain = fopen("data.txt", "r+");
if (fmain == NULL)

printf("Can'topen file.n"); exit(1);

fnew = fopen("i.txt", "r+");
if (fnew == NULL)

printf("Can'topen file.n"); exit(1);

...





share|improve this answer






















  • Thank you for but the answer but that was not the problem. I fixed it and it still not working
    – AlexS
    Nov 12 at 15:08













1












1








1






  1. There is no "rw+" mode for fopen, you probably want the "r+" mode.

  2. You need to check if fopen was successful.

Try this:



 ...
fmain = fopen("data.txt", "r+");
if (fmain == NULL)

printf("Can'topen file.n"); exit(1);

fnew = fopen("i.txt", "r+");
if (fnew == NULL)

printf("Can'topen file.n"); exit(1);

...





share|improve this answer














  1. There is no "rw+" mode for fopen, you probably want the "r+" mode.

  2. You need to check if fopen was successful.

Try this:



 ...
fmain = fopen("data.txt", "r+");
if (fmain == NULL)

printf("Can'topen file.n"); exit(1);

fnew = fopen("i.txt", "r+");
if (fnew == NULL)

printf("Can'topen file.n"); exit(1);

...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 8:13

























answered Nov 12 at 8:00









Jabberwocky

26.5k93769




26.5k93769











  • Thank you for but the answer but that was not the problem. I fixed it and it still not working
    – AlexS
    Nov 12 at 15:08
















  • Thank you for but the answer but that was not the problem. I fixed it and it still not working
    – AlexS
    Nov 12 at 15:08















Thank you for but the answer but that was not the problem. I fixed it and it still not working
– AlexS
Nov 12 at 15:08




Thank you for but the answer but that was not the problem. I fixed it and it still not working
– AlexS
Nov 12 at 15:08













0














You have to first check if the file you opened is not returning NULL and then only you can write to the file. As in here, the size of the data is fixed, write the entire array into a file is using the binary writing mode:



fp = fopen("data2.txt", "wb");
if(fp == NULL)

printf("Error!");
exit(1);

fwrite(arr, sizeof(char), sizeof(arr), fp);
fclose(fp);





share|improve this answer




















  • thank you for the answer. however, the data2.txt is still empty after i run the program
    – AlexS
    Nov 12 at 6:42















0














You have to first check if the file you opened is not returning NULL and then only you can write to the file. As in here, the size of the data is fixed, write the entire array into a file is using the binary writing mode:



fp = fopen("data2.txt", "wb");
if(fp == NULL)

printf("Error!");
exit(1);

fwrite(arr, sizeof(char), sizeof(arr), fp);
fclose(fp);





share|improve this answer




















  • thank you for the answer. however, the data2.txt is still empty after i run the program
    – AlexS
    Nov 12 at 6:42













0












0








0






You have to first check if the file you opened is not returning NULL and then only you can write to the file. As in here, the size of the data is fixed, write the entire array into a file is using the binary writing mode:



fp = fopen("data2.txt", "wb");
if(fp == NULL)

printf("Error!");
exit(1);

fwrite(arr, sizeof(char), sizeof(arr), fp);
fclose(fp);





share|improve this answer












You have to first check if the file you opened is not returning NULL and then only you can write to the file. As in here, the size of the data is fixed, write the entire array into a file is using the binary writing mode:



fp = fopen("data2.txt", "wb");
if(fp == NULL)

printf("Error!");
exit(1);

fwrite(arr, sizeof(char), sizeof(arr), fp);
fclose(fp);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 6:24









Busy Bee

9571619




9571619











  • thank you for the answer. however, the data2.txt is still empty after i run the program
    – AlexS
    Nov 12 at 6:42
















  • thank you for the answer. however, the data2.txt is still empty after i run the program
    – AlexS
    Nov 12 at 6:42















thank you for the answer. however, the data2.txt is still empty after i run the program
– AlexS
Nov 12 at 6:42




thank you for the answer. however, the data2.txt is still empty after i run the program
– AlexS
Nov 12 at 6:42



這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands