3
Hello. I have a question how to read numbers from a given file in c to put inside the loop
The file . txt will have the following data:
3
Joao Maria
branco p
Marcio Guess
vermelho p
Maria Jose
branco p
2
Joao Losna
Vermelho G
Donald Trump
Branco P
0
You would have to read the number 3, put inside the repeat loop, read color/size name save in variables.
Read the next number which is the 2 put inside the repeat loop, read the data.
Until the number is 0 as in the example.
Part of code in c:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char *nome[100];
char cor[100];
char tam[100];
} Camisetas;
int comparaNome(const void *a, const void *b) {
const Camisetas *elemA = a;
const Camisetas *elemB = b;
int valor;
if (elemA->cor[0] > elemB->cor[0])
valor = 1;
else if (elemA->cor[0] < elemB->cor[0])
valor = -1;
else if (elemA->tam[0] < elemB->tam[0])
valor = 1;
else if (elemA->tam[0] > elemB->tam[0])
valor = -1;
else if (elemA->nome[0] > elemB->nome[0])
valor = 1;
else if (elemA->nome[0] < elemB->nome[0])
valor = -1;
else
valor = 0;
return valor;
}
int main() {
size_t len = 100; // valor arbitrário
Camisetas pessoas[100];
char c, *linha = malloc(len), *nome = malloc(len), *cor = malloc(len), *tam = malloc(len);
int casos[100], i, j, k, C, aux, aux2;
char url[] = "doc.txt";
FILE *arq = fopen(url, "r");
arq == NULL ? printf("Erro, nao foi possivel abrir o arquivo\n") : printf("Sucesso ao abrir o arquivo\n");
//pegar os casos
for (j = 0; j < 2 && !feof(arq); j++) {
fscanf(arq, "%c", &C);
casos[j] = atoi(&C);
if (casos[j] != 0) {
aux2 = casos[j];
}
aux = C * 2;
}
while (casos) {
for (i = 0; i < aux && !feof(arq); i++) {
getline(&pessoas[i].nome, &len, arq);//ou usar fscanf
(fscanf(arq, "%s %s\n", &pessoas[i].cor, &pessoas[i].tam) != EOF);
}
qsort(pessoas, aux2, sizeof(Camisetas), comparaNome);
for (k = 0; k < aux2; k++) {
printf("%s %s %s", pessoas[k].cor, pessoas[k].tam, *pessoas[k].nome);
}
}
return 0;
}
Look at these parts:
else if (elemA->cor[0] cor[0])
,else if (elemA->tam[0] tam[0])
andelse if (elemA->nome[0] nome[0])
- that does not compile.– Victor Stafusa
Ignore my previous comment. Is that you had put the code inside tags
<pre><code>...</code></pre>
. Don’t do that. Instead of using these tags, you have four spaces. You can use the button{}
from the editor after selecting all your code to do this.– Victor Stafusa
Why in the role
comparaNome
you look only at the first letter?– Victor Stafusa
Why do you use the asterisk on
char *nome[100];
, but not inchar cor[100];
orchar tam[100];
?– Victor Stafusa
hello Victor, yes the function comparedName looks only at the first letter to after alphabetizing the names. take a look at <https://www.urionlinejudge.com.br/judge/pt/problems/view/1258> and <http://www.urionlinejudge.com.br/forum/viewtopic.php?f=5&t=260> the codes are for this problem.
– carlos cunha
@Carloscunha can’t go around changing the question to a new one, if you have new questions, create a new one with reference to this one. Otherwise the answers may lose their validity. Remembering that here is not a forum but a site of Questions and Answers. Regards.
– Jorge B.