Help with reading files, and other stuff..

This is a discussion on Help with reading files, and other stuff.. within the C Programming forums, part of the General Programming Boards category; Hello to everyone. I'm having some problems regarding reading specific data from a .txt file(C). I need to read, for ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Help with reading files, and other stuff..

    Hello to everyone. I'm having some problems regarding reading specific data from a .txt file(C). I need to read, for example:
    PHP Code:
    1 50 5
    Gabriel Stewie Stan Roger Peter
    4 2
    Stewie 10 12 P 5 O 6 E 8 O 5 C
    Stan 7 5 E 4 C 13 O 7 O 6 C
    Roger 10 6 P 2 P 5 P 3 P 4 P
    Peter 2 7E 3C 2E 1O 9P 
    But I need to put each data into a single variable. I've thought about using something like:
    Code:
    do
    {
    ...
    }while (!feof(p)); /* || c !=' ' || c*!='\n'[/PHP]
    But it is not working at all.. If anyone has any idea of how getting this to work, I'll be glad to try it ^^
    
    Another thing.. While the file reading part is not ready, I'm trying to build the rest of my code using scanf(...). This is the code so far:
    [PHP]int main()
    {
        int n, jog, rjog, di, pin, dround; 
        int i,j,k,card,auxjog; 
        char nom[30];
        Jogador player[5], aux, vencedor;
    
        printf("Rodadas:");
        scanf("%d",&n);
        printf("dinheiro inicial:");
        scanf("%d",&di);
        printf("nº de jogadores:");
        scanf("%d",&jog);
    
        for (j=0; j<jog; j++) 
        {
            printf("nome jogador %d:",j+1);
            scanf("%s",player[j].nome);
            player[j].dtotal=di;
        }
    
    
        for (i=1; i<=n; i++) 
        {
            printf("jogadores da rodada:");
            scanf("%d",&rjog);
            printf("pingo:");
            scanf("%d",&pin);
            dround=0;
    
            for (j=0; j<jog; j++)  
            {
                if (player[j].dtotal<=pin) 
                {
                player[j].dtotal=player[j].dtotal-pin;
                dround=dround+pin;
                }
                else if (player[j].dtotal!=0) 
                {
                    dround=dround+player[j].dtotal;
                    player[j].dtotal=0;
                }
            }
    
            for (j=0; j<rjog; j++) 
            {
                printf("nome jogador %d:",j+1);
                scanf("%s",nom);
    
                for (k=0; k<rjog; k++) 
                {
                    auxjog=0;
                    if (player[k].nome==nom)
                    {
                        player[auxjog]=aux;
                        player[k]=player[auxjog];
                        aux=player[k];
                        auxjog=auxjog+1;
                    }
                }
    
                for (card=0; card<=4; card++) 
                {
                    printf("Carta %d:",card+1);
                    scanf("%d",player[j].carta[card].numero);
                    printf("Naipe da carta %d:",card+1);
                    scanf("%c",player[j].carta[card].naipe);
                }
            }
        }
    return 0;
    }
    Code:
    typedef struct carta
    {
        int numero;
        char naipe;
    }Carta;
    
    typedef struct jogador
    {
        char nome[30];
        Carta carta[5];
        int dtotal;
        int aposta;
    }Jogador;
    It is not ready yet, but I'm having some problems on the last "for" of main.c. Any ideas?
    By the way, if anyone hasn't noticed yed, I'm kinda new to C.. ^^'

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    some problems on the last "for" of main.c
    What problems?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    There is nothing wrong with the compilation, but the program stops working as soon as it gets there.

    Just to be more specific, this one:
    Code:
    for (card=0; card<=4; card++) 
                {
                    printf("Carta %d:",card+1);
                    scanf("%d",player[j].carta[card].numero);
                    printf("Naipe da carta %d:",card+1);
                    scanf("%c",player[j].carta[card].naipe);
                }
    Can anyone find any problem in it?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Congratulations, you've inherrited what I would consider a nightmare file to read...

    There isn't any real patter to the data. Other than each line starting with a string, those numbers and latters appear totally random in that there's no way to say "Name, two numbers, letter, three numbers, letter, two numbers" and have it hold true for each line.

    Unless you know what's what inside a file it's going to be very hard to read...
    Last edited by CommonTater; 04-17-2011 at 09:57 PM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Do you mean putting the &, like this?
    Code:
    for (card=0; card<=4; card++) /*Le as cartas na mão de cada jogador da rodada*/
                {
                    printf("Carta %d:",card+1);
                    scanf("%d",&player[j].carta[card].numero);
                    printf("Naipe da carta %d:",card+1);
                    scanf("%c",&player[j].carta[card].naipe);
                }
    I mean, this does solve the warning, but for some reason it is not executing the second scanf()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read, Write on files.. counting lines.. stuff like that
    By Darkobra in forum C Programming
    Replies: 14
    Last Post: 07-14-2010, 03:20 PM
  2. Reading PDF and DOC files
    By kleanchap in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2010, 09:32 AM
  3. Reading Through Files
    By PLC in forum C Programming
    Replies: 6
    Last Post: 05-13-2004, 01:25 AM
  4. reading files
    By stevew2607 in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2002, 05:31 PM
  5. Adding stuff to strings from files..
    By R@m0n in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2002, 02:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21