Place and get file integers?

2

I have two integer variables at rp and rc , and I have to write them to a file as a way to save their value, and then get them and put them into variables of type inteiro to use in my program.

To write to a file, I'm just using an escape character to let me know if the number is gone or not. This character is the semicolon ; .

I'm trying with the following code, where arq is my file:

To get value:

cc = 1;
while(! feof(arq))
{
    get = fgetc(arq);
    if(get != ';')
    {
        if(cc == 1)
        {
            rc = (int)get;
            cc ++;
        }
        else
            rp = (int)get;
        }
}
fclose(arq);

To place:

fputc(rc, arq);
fputc(';', arq);
fputc(rp, arq);
fputc(';', arq);

I know where the error is, but I do not know how to fix it. It is taking the value ASCII of the character - when I put an integer it takes the value ASCII and not the number, and when it happens the same happens, it converts the integer to char and uses ASCII .

Another problem is that I'm only taking one-digit numbers into account; how do I take larger numbers into account?

    
asked by anonymous 31.10.2015 / 03:51

1 answer

1

The practical solution

You should know a bit of file manipulation and IO, and their syntax in C. To read, you should just check where ';' appears, and at these intervals use fscanf with integer syntax. For writing, use fprintf with string syntax, for each printed number, print the break character.

The manual solution

The first step is to know the size of the number. For this you iterate character by character until you find your break character (which in this case is ';' ). To do this, you are picking up characters (using fgetc ) one by one and compares with the break character. If it is not, you must remove the value equivalent to the '0' character. This is what you forgot to do. And then add a FIFO framework . When you find the break character, you instantiate queue and do the same thing (this kind of thing is easier to do with object-oriented). Once you have all these queues, you use a concept from our number system. For example, 21 = 2*10^1 + 1*10^0 , 432 = 4*10^2 + 3*10^1 + 2*10^0 . If we realize that the values are multiplied by powers of 10, where the largest digit is multiplied by 10 ^ (position - 1). So you use this logic:

enquanto o arquivo não estiver no fim:
    caractere = arquivo.le_caractere()
    enquanto caractere não for ';'
        queue.push(caractere - '0')
        caractere = arquivo.le_caractere()
    lista_de_queues += queue

para cada queue em lista_de_queues:
    enquanto houver itens no queue:
        numero += queue.pop() * 10^queue.size()
    lista_de_numeros += numero

Note that queue.size () is necessarily the old size - 1, because by precedence, pop is called first. That way you can read the numbers.

Fortunately, saving is even easier! You must do the opposite operation to extract a digit, and then save it as a char, added to '0' . The number of digits in a number is his logarithm in base 10. Make this an integer, and do the following using a LIFO structure :

for(unsigned i = 1; i <= digitos; i++)
    stack.push( int( (numero % 10^i) / 10^(i-1) ) )
// nesse ponto, estarão na pilha, na ordem inversa. como é uma estrutura LIFO,
// ele vai nos devolver os dígitos na ordem correta
enquanto houver itens na pilha:
arquivo.escreve_caractere(stack.pop() + '0') 

Negative numbers, just check if the first character is '-' . If it is, multiply the final number by -1.

    
13.01.2016 / 16:15