Write doubles with fwrite in c

2

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.

    
asked by Patricio 17.04.2018 в 00:56
source

2 answers

1
  

I'm using the fwrite function in C to write double data to a text file, but when I open the file I do not read the numbers but a series of symbols and when I use binary files for writing or reading there's no problem

It is something to be expected. fwrite is a binary writing utility, that is, it does not treat the data to be saved in any way.

If you use fwrite with in your example:

fwrite( &dinero[0], sizeof(unsigned int), 10, fp );

What will be saved in the file is the binary representation of dinero[0] , that is, of 23. In the file the following hexadecimal sequence would be saved:

0000 0000 0000 0017

If you try to open this file in text mode you will have 7 null characters and a last reserved character whose meaning would be End of transaction block . I mean ... nothing readable.

To recover the information stored in binary you have to resort to fread that reads the data in binary form ... so, when storing the previous sequence in the memory associated with a variable of type unsigned int , your application will understand that in this variable is the original 23 number.

In order for the data in the file to be readable by a human it is necessary to save them in text format and this implies giving some treatment to the data ... you can not save your binary representation but you have to use one that is readable ... a solution would be to convert the numbers to text and for that you can use, as you said @DarielRamosDiazVillegas , fprintf

    
answered by 17.04.2018 / 08:13
source
1

You can use the function fprintf that is similar to printf only that the output does it in the file you pass as the first example parameter:

FILE *fp;

unsigned int dinero[10] = { 23, 12, 45, 345, 512, 345, 654, 287, 
567, 124 };

char str[] = "This is tutorialspoint.com";

fp = fopen( "file.txt" , "w" );
int i = 0;
//Recorro el arreglo y voy añadiendo cada número si deseara solo imprimir 1 ejecuto el cuerpo del ciclo for
// solamente e indico el indice del arreglo que quiero imprimir en el archivo
for(i=0;i<10;i++){
    fprintf(fp, (i != 9) ? "%d, ": "%d", dinero[i]);
}
fclose(fp);
    
answered by 17.04.2018 в 01:30