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

Asked

Viewed 80 times

0

My teacher passed this code that writes to the txt file. Inserts random number. After executing this code he wants me to sum the values written in txt, but they were written in a char vector and for me to do the sum I need to convert them. And show that sum on the screen. 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;

Follows code:

 #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);

}

  • I didn’t understand very well what you want to do? Could you be more specific and add more details to the question?

  • Specific*. Right I’ll try.

  • I’m sorry, I didn’t know it was a girl. Like this text file and how would you like it to be the output?

  • :) All right. I edited.

1 answer

1


First you have higher values than 255 so you can’t use a vector char its vector must be a int.

int vetor[50];

To make the sum, use a for within the while.

int line;
while(!feof(arq)){
    //...
    line = 0;
    for(i=0;x<8;i++){
        line += vetor[i]
    }
    printf("Soma da linha: %d\n", line)
}
  • Bigger than 255, where? I don’t understand.

  • 1

    @Soon in the first line you have 333;60;565;... and the char variable only supports values up to 255, so its vector must be integer

  • I don’t know about that.

  • @gonz: Why do you question the answer as correct if you didn’t understand and she didn’t solve your problem? This makes the person who might have understood your problem better, and perhaps they could explain to you the relationship of "char" with 255 respond, since you already have an accepted answer. Clear your doubts, do what Voce has to work, and then yes accept the answer.

  • That’s not it. She solved my problem, I just didn’t get confident with the 255. I have to study about it.

  • Types char are bytes, in most implementations of C language. And bytes have 8 bits. And each bit can assume two values. Two high to eight is 256. Bytes allow to represent up to 256 states. If the first state is 0, the following will be 1, and then 2, up to 255.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.