Read file and write .txt files in C language

2

The following is to see what is my failure to write a code because I know that there is interaction (reading) between the program and the .odt file, which is the case that I tried, but I bring the text in strange symbols and not in letters. In the file that I named test.odt and it contains a line that says " testing read in C ", create the following code:

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

int main()
{
    FILE *archivo;
    char caracter;

    archivo = fopen("prueba.odt","r");

    if (archivo == NULL)
        {
            printf("\nError de apertura del archivo. \n\n");
        }
        else
        {
            printf("\nEl contenido del archivo de prueba es \n\n");
            while((caracter = fgets(archivo)) != EOF)
        {
        printf("%s",caracter);
        }
        }
        fclose(archivo);
    return 0;
}

I know that the program reads the file, but as I said before, it brings an unintelligible content.

    
asked by HJR_Caracas 19.04.2017 в 19:00
source

1 answer

7

The .odt file format, similar to Word .doc or .docx , is not a text "puro" , but (in general [*]) is a binary format that the corresponding application (in this case, LibreOffice or similar) He knows how to interpret. You can not pretend to read byte by byte and print it to the text console and wait for the result to be readable (what would you expect to read when the odt contains letters in different sizes and formats, not to mention embedded images, etc.?). Use a pure text file (usually have extension '.txt', although this is not necessary or sufficient). And before you schedule something like that, make sure you know what the file contains, examine it with a binary viewer .

[*] In fact, the format can be text (if XML is used), but even then you would not see the text you see in the word processor, but a lot of ... "weird text" . On the other hand, an odt file is almost always a compressed file that contains other files inside. You can check it by simply changing the extension to ".zip" and opening it. You can also guess if you see that the first two bytes correspond to the characters "PK" (relic of PkZip)

    
answered by 19.04.2017 / 19:33
source