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?
– gato
Specific*. Right I’ll try.
– Aline
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?
– gato
:) All right. I edited.
– Aline