#FILES using

38 messages · Page 1 of 1 (latest)

placid thicket
#

Hello i need to fscanf this file and put it on a linked list here is the file below:

so far here is what i did :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct p{
char post[2];
char name[20];
char club[20];
}p;

int main()
{
FILE *fp;

fp = fopen("player.txt","r");

char next_word[20];

    while (fscanf(fp, "%s", next_word) != EOF)
{
    p *n = malloc(sizeof(p));
    if (n == NULL)
    {
        return 0;
    }
    
    strcpy(n->post, next_word);
  printf("%s",n->post);


return 0;

}
}

the issue here is that it only scanf the Gk and not the other things that i want to put on the list anyone know can i solve this ?

merry stirrupBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

echo pelican
#

Post the contents of file player.txt here too.

placid thicket
#

i did didnt I ?

sturdy flower
#

the actual text

echo pelican
#

One day, I will learn how to read.
Apos

sturdy flower
#

there's a lot of mobile users here

placid thicket
#

GK: Emi Martinez (Aston Villa)
GK: Franco Armani (River Plate)
GK: Geronimo Rulli (Villarreal)
DF: Marcos Acuna (Sevilla)
DF: Juan Foyth (Villarreal)
DF: Lisandro Martinez (Manchester United)
DF: Nicolas Tagliafico (Lyon)
DF: Cristian Romero (Tottenham)
DF: Nicolas Otamendi (Benfica)
DF: Nahuel Molina (Atletico Madrid)
DF: Gonzalo Montiel (Sevilla)
DF: German Pezzella (Real Betis)
MF: Angel Di Maria (Juventus)
MF: Leandro Paredes (Juventus)
MF: Rodrigo De Paul (Atletico Madrid)
MF: Alexis Mac Allister (Brighton)
MF: Enzo Fernandez (Benfica)
MF: Exequiel Palacios (Bayer Leverkusen)
MF: Guido Rodriguez (Real Betis)
FW: Lionel Messi (PSG)
FW: Lautaro Martinez (Inter Milan)
FW: Paulo Dybala (Roma)
FW: Angel Correa (Atletico Madrid)
FW: Julian Alvarez (Manchester City)
FW: Thiago Almada (Atlanta United)
FW: Alejandro Gomez (Sevilla)

#

here is the text

#

and dw hahaha

echo pelican
#

I am wondering if it would make more sense to do a fgets() for each line and then run sscanf() over said line.

placid thicket
#

never actually worked with sscanf hold on let me see what it do

echo pelican
#

fscanf() and sscanf()
Just the same, different source.

But doing an fgets() per line is more robust, because you cannot as easily derail that process with incomplete or corrupted lines in the file.

placid thicket
#

yh i see what you are saying imma try it with like that

echo pelican
#

Hold on though, I think you may have your formatting wrong.

placid thicket
#

fgets(string,30,fp) sscanf(string,%s %s %s %s,n->post,n->fname,n->lname,n->club) should work like that right

#

Have also tght about using strtok in order to divide the file but seems complicated

echo pelican
#

Something like that, but you can/should limit string length, e.g. %20s for the name and for the club.

placid thicket
#

well i actually tried like that :while (fgets(next_word,20, fp))
{
p *n = malloc(sizeof(p));
if (n == NULL)
{
return 0;
}

    sscanf(next_word, "%s %s %s %20s",n->post,n->fname,n->lname,n->club);
  printf("%s",n->club);
#

but it doesnt output lets say for example the club

#

here is what it did output for n->club

#

(A

#

it just took the 2 first digit is there a way to fix it ?

#

Lname works perfectly tho

#

post and fname not that much also

echo pelican
#
char pos[2];
char fname[20];
char lname[20];
char club[20];

if (sscanf(line, "%2s: %20s %20s ((%20[^)])", pos, fname, lname, club) == 4) {
    printf("pos = %s\n", pos);
    printf("name = %s %s\n", fname, lname);
    printf("club = %s\n", club);
}
#

And you could always follow up with

else if (sscanf(line, "%2s: %20s ((%20[^)])", pos, lname, club) == 3) {
    printf("pos = %s\n", pos);
    printf("name = %s\n", lname);
    printf("club = %s\n", club);
}

For players that go by just one name.

#
MF: Alexis Mac Allister (Brighton)

Motherf...

#

I think you're going to have to do strtok() the lines.
That, or find a regex C library (nuclear option)

placid thicket
#

hahahahahaha yh i guess so

#

it does cover more option

#

not really familiar with strtok so i prefer to avoid it but i will look more into it

echo pelican
#

Well, you can key on the : and ().
Shouldn't be too difficult?

placid thicket
#

yep should be fine

echo pelican
#

I do suggest you fgets() into a line buffer that is a bit larger than 30 chars though.
But I reckon you sussed that already.

placid thicket
#

hahahahaha yh thanks you also for your insight it helped a lot

merry stirrupBOT
#

@placid thicket Has your question been resolved? If so, run !solved :)

placid thicket
#

!solved