Reading of files in C

1

I am trying to make a program which contains the contents of a file and relates its contents to a structure because it has two fields that I want to save, the first field that contains the character and the second field that contains the code of that character . For example:

P: 00 N: 01 Z: 10

My problem is that the program must save any type of character, including the end of line character (\ n), in that case being two complicates everything. In these moments I do the reading with "fscanf (dictionary,"% c:% s ", letter, code);" but in the aforementioned case, I read the "\" as a character and the "n" as a different one. I thought about reading character character but I can not find the way to relate it later with the structure. If you could help me with some idea I would appreciate it. I leave the code that I currently use:

void guarda_diccionario(char diccionariotxt[20]){
    int i=0, nro_elementos=0;
    FILE * diccionario;

    diccionario = fopen(diccionariotxt, "r");       //Abrimos el diccionario para asignar cada campo a la estructura.
    if(!diccionario){
        printf("Archivo invalido...\n");
        return;
    }

    if(diccionario != NULL){
        while(!feof(diccionario)){          //Empieza a guardar cada campo.
            fscanf(diccionario, "%c:%s", &nodo[nro_elementos].letra,&nodo[nro_elementos].codigo);
            nro_elementos++;
        }
    fclose(diccionario);
    }
}
    
asked by Marshal 26.06.2016 в 03:55
source

1 answer

3
  

My problem is that the program must save any type of character

And that, it's a terrible problem. Especially considering that apparently each of the characters has semantic meaning.

leonbloy has pointed in the right direction: you're unclear about what a character is em>. Let's look at the table of characters : We can see that there is a character for the carriage return whose code is 10 decimal ( 0a hexadecimal or 00001010 binary). It is not possible to express such a character directly in the code because it produces a compilation error, as seen here:

char frase[] = "En un lugar de La Mancha
de cuyo nombre no quiero acordarme";

The above code causes compilation error because the compiler considers that the instruction ends after "La Mancha" , that is: just in the carriage return ... the compiler sees an instruction that does not end in a semicolon ( ; ) or close the text string with the closing character ( " ).

Since you can not write a carriage return literally in the text string, you have to find a way to write it without affecting how the compiler will interpret the code ... for that they invented the escape sequences . In as in other languages of your family, the sequence of Escape starts with the bar \ followed by the code of the character you want to write, so the carriage return is the \n that you already know, the n belongs to "new line", with it we can correct the previous error:

char frase[] = "En un lugar de La Mancha\nde cuyo nombre no quiero acordarme";

But we should not confuse the escape sequence with a sequence of characters; the \n in the example text string is a single character (the carriage return character) not a two character string (the% bar% co_and the%% co_%).

Now let's go back to your text file, suppose its content is as follows:

P:00 N:01 Z:10 \n:11

This is 20 characters (bytes):

byte        |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|
caracter    | P| :| 0| 0|  | N| :| 0| 1|  | Z| :| 1| 0|  | \| n| :| 1| 1|
hexadecimal |50|3a|30|30|20|4e|3a|30|31|20|5a|3a|31|30|20|5c|6e|3a|31|31|

If you want a carriage return you must write it explicitly, because unlike a C source code it will not affect the compilation:

P:00 N:01 Z:10 
:11

This is 19 characters (bytes):

byte        |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|
caracter    | P| :| 0| 0|  | N| :| 0| 1|  | Z| :| 1| 0|  |\n| :| 1| 1|
hexadecimal |50|3a|30|30|20|4e|3a|30|31|20|5a|3a|31|30|20|0a|3a|31|31|
    
answered by 27.06.2016 в 16:38