after fwrite destination file is empty c language [closed]
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
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
|
show 1 more comment
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
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
You're writing it todata2.txtand 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 withdata.txthaving1,i.txthaving2and 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 ofdata2.txt.
– Antti Haapala
Nov 12 at 6:56
|
show 1 more comment
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
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
c fwrite
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
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
You're writing it todata2.txtand 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 withdata.txthaving1,i.txthaving2and 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 ofdata2.txt.
– Antti Haapala
Nov 12 at 6:56
|
show 1 more comment
You're writing it todata2.txtand 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 withdata.txthaving1,i.txthaving2and 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 ofdata2.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
|
show 1 more comment
2 Answers
2
active
oldest
votes
- There is no
"rw+"mode forfopen, you probably want the"r+"mode. - You need to check if
fopenwas 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);
...
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
add a comment |
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);
thank you for the answer. however, the data2.txt is still empty after i run the program
– AlexS
Nov 12 at 6:42
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
- There is no
"rw+"mode forfopen, you probably want the"r+"mode. - You need to check if
fopenwas 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);
...
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
add a comment |
- There is no
"rw+"mode forfopen, you probably want the"r+"mode. - You need to check if
fopenwas 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);
...
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
add a comment |
- There is no
"rw+"mode forfopen, you probably want the"r+"mode. - You need to check if
fopenwas 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);
...
- There is no
"rw+"mode forfopen, you probably want the"r+"mode. - You need to check if
fopenwas 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);
...
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
add a comment |
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
add a comment |
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);
thank you for the answer. however, the data2.txt is still empty after i run the program
– AlexS
Nov 12 at 6:42
add a comment |
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);
thank you for the answer. however, the data2.txt is still empty after i run the program
– AlexS
Nov 12 at 6:42
add a comment |
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);
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);
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
add a comment |
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
add a comment |
You're writing it to
data2.txtand 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.txthaving1,i.txthaving2and 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