The function writes to the file in txt. I need to do the sum but the vector is like char

0

My teacher passed this code that writes to the txt file. Enter random number. After running this code it wants me to do the sum of the values written in the txt, but they were written in a vector of char and for me to make the sum I need to convert them. And show that sum on the screen. Do you understand?

Text written in txt:

333;60;565;212;657;95;364;486;860;
68;859;649;573;475;631;389;698;467;
952;14;834;912;163;678;929;518;85;
151;123;413;76;435;948;405;576;312;
608;708;339;814;582;278;36;498;218;
788;234;728;300;368;151;444;339;767;
75;42;141;368;163;912;971;469;1;
760;665;136;614;938;460;320;118;473;
599;795;411;876;79;590;644;64;910;

Follow the code below:

 #include<stdio.h>
 #include<time.h>

void main(){
int i, j , x;
char vetor[50];
FILE *arq;

arq = fopen("dados.txt", "w");
srand(time(NULL));

    for(i = 0; i < 9; i++){
        for(j = 0; j < 9; j++){
            x = rand() % 1000 +1;
            fprintf(arq, "%d;", x);
        }
    fprintf(arq, "\n");
    }
fclose(arq);

//Faz a leitura:

arq = fopen("dados.txt", "r");

while(!feof(arq)){
    fscanf(arq, "%d;%d;d%;%d;%d;%d;%d;%d;%d;%d;",&vetor[0], &vetor[1], &vetor[2], &vetor[3], &vetor[4], &vetor[5], &vetor[6], &vetor[7], &vetor[8] );

    for(i = 0; i<4; i++){
        printf("[%d]=%d\t", i, vetor[i]);           
    }
    printf("\n");
}
fclose(arq);

}

    
asked by anonymous 02.06.2016 / 01:13

1 answer

1

First you have values greater than 255 then you can not use a vector char your vector must be a int .

int vetor[50];

To make the sum, use a for within while .

int line;
while(!feof(arq)){
    //...
    line = 0;
    for(i=0;x<8;i++){
        line += vetor[i]
    }
    printf("Soma da linha: %d\n", line)
}
    
02.06.2016 / 13:01