I'm going to ask a question maybe a newbie. I am using the fwrite
function in C to write double
data to a text file, but when I open the file I do not read numbers but a series of symbols and when I use binary files for writing or reading there is no problem (because I do not open them in the text editor), but if I use text files it gives me problems writing data int
or double
. My sample code:
#include <stdio.h>
int main ( int argc, char **argv )
{
FILE *fp;
unsigned int dinero[10] = { 23, 12, 45, 345, 512, 345, 654, 287,
567, 124 };
fp = fopen ( "fichero.txt", "w" );
fwrite( &dinero[0], sizeof(unsigned int), 10, fp );
fclose ( fp );
return 0;
}
All the codes of examples on the web are like this. Nowhere did I find information on whether that function is used only with binary files.
Thank you very much already.
Well, I'm going to tell you a little about what I see. Just for contributing something. When I open the file with the gedit editor and write only text, the text is read correctly. If I wrote text and numbers either int, float or double the text is read but instead of numbers appear disordered symbols (apparently). If I open it with the sublime text editor and the file contains text and numbers, what you see is the following:
4d6f 7374 7261 6e64 6f20 656c 2075 736f 2064 6520 6677 7269 7465 2065 6e20 756e 2066 6963 6865 726f 2e0a 0100 0000
For what you find is the ascii code of the text plus the number. If the file contains only text, the text is normally read. And if the file contains only sublime text numbers it shows me the following:
0100 0000
Enter a single number that is 1. The type of data entered is unsigned int.I look for it to be a binary or something but not. If it is double the file has something else. Well I really went by the bush and my intention was to contribute something but maybe it does not help. My doubt is that I can not see the numbers written in a text file if I use the fwrite function. Well my conclusion is that the text can be written in txt files but the numbers should be written in binary files. 0100 0000 is 1 in hexadecimal. The integers occupy 4 bytes and I have two digits per byte. I can use the function to write and to read it from the file with fread I keep it in an arrangement to work with the numbers. The rest was just curious because I can not see the text file correctly when I enter numbers.