Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:45:07+00:00 2026-06-04T21:45:07+00:00

I wrote this simple program in C, because I’m studying FILES right now at

  • 0

I wrote this simple program in C, because I’m studying FILES right now at University. I take a txt file with a list of the results of the last race so my program will show the data formatted as I want. Here’s my code:

/* Esercizio file Motogp */
#include <stdio.h>
#define SIZE 20

int main ()
{
    int pos, punt, num;
    float kmh;
    char nome[SIZE+1], cognome[SIZE+1], moto[SIZE+1];
    char naz[SIZE+1], nome_file[SIZE+1];

    FILE *fp;

    printf ("Inserisci il nome del file da aprire: ");
    gets (nome_file);

    fp = fopen (nome_file, "r");

    if (fopen == NULL)
        printf ("Errore nell' apertura del file %s\n", nome_file);
    else {

        while (fscanf (fp, "%d %d %d %s %s %s %s %.2f",
                &pos, &punt, &num, nome, cognome, naz, moto, &kmh) != EOF ) {

              printf ("Posizione di arrivo: %d\n", pos);
              printf ("Punteggio: %d\n", punt);
              printf ("Numero pilota: %d\n", num);
              printf ("Nome pilota: %s\n", nome);
              printf ("Cognome pilota: %s\n", cognome);
              printf ("Nazione: %s\n", naz);
              printf ("Moto: %s\n", moto);
              printf ("Media Kmh: %d\n\n", kmh);
        }
    }

    fclose(fp);

    return 0;
}

and there’s my txt file:

1   25  99  Jorge LORENZO    SPA    Yamaha  164.4   
2   20  26  Dani PEDROSA     SPA    Honda   164.1   
3   16  4   Andrea DOVIZIOSO ITA    Yamaha  163.8   
4   13  1   Casey STONER     AUS    Honda   163.8   
5   11  35  Cal CRUTCHLOW    GBR    Yamaha  163.6   
6   10  19  Alvaro BAUTISTA  SPA    Honda   163.5   
7   9   46  Valentino ROSSI  ITA    Ducati  163.3   
8   8   6   Stefan BRADL     GER    Honda   162.9   
9   7   69  Nicky HAYDEN     USA    Ducati  162.5   
10  6   11  Ben SPIES    USA    Yamaha  162.3   
11  5   8   Hector BARBERA   SPA    Ducati  162.1   
12  4   17  Karel ABRAHAM    CZE    Ducati  160.9   
13  3   41  Aleix ESPARGARO  SPA    ART 160.2   
14  2   51  Michele PIRRO    ITA    FTR 160.1   
15  1   14  Randy DE PUNIET  FRA    ART 160.0   
16  0   77  James ELLISON    GBR    ART 159.9   
17  0   54  Mattia PASINI    ITA    ART 159.4   
18  0   68  Yonny HERNANDEZ  COL    BQR 159.4   
19  0   9   Danilo PETRUCCI  ITA    Ioda    158.2   
20  0   22  Ivan SILVA   SPA    BQR 158.2

When I run my program, it return me an infinite loop of the first one. Why?
Is there another function to read those data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:45:08+00:00Added an answer on June 4, 2026 at 9:45 pm

    This one works (borrowed from @Mahmoud Al-Qudsi; I don’t want any points for this)

    #include <stdio.h>
    #define SIZE 20
    
    int main (int argc, char **argv)
    {
        int pos, punt, num;
        float kmh;
        char nome[SIZE+1], cognome[SIZE+1], moto[SIZE+1];
        char naz[SIZE+1];
    
        FILE *fp;
    
        fp = fopen (argv[1], "r");
    
        if (!fp ) { printf ("Errore nell' apertura del file %s\n", argv[1]); return 0; }
    
            while (fscanf (fp, "%d %d %d %s %s %s %s %f\n",
                    &pos, &punt, &num, nome, cognome, naz, moto, &kmh) == 8 ) {
    
                  printf ("Posizione di arrivo: %d\n", pos);
                  printf ("Punteggio: %d\n", punt);
                  printf ("Numero pilota: %d\n", num);
                  printf ("Nome pilota: %s\n", nome);
                  printf ("Cognome pilota: %s\n", cognome);
                  printf ("Nazione: %s\n", naz);
                  printf ("Moto: %s\n", moto);
                  printf ("Media Kmh: %f\n\n", kmh);
            }
    
        fclose(fp);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to retrieve folders name from a zip file. I wrote this simple
I wrote this simple Prolog program. man(socrates). mortal(X) :- man(X). immortal(X) :- immortal(X). I
I wrote this simple code (in python) in test.py. I try to run timeit
I wrote this simple C code and compiled it using Visual Studio 2010, with
I've wrote this simple code to attach events to one of my divs. But
So I wrote this simple console app to aid in my question asking. What
I've wrote this simple piece of code. And I have a slight problem with
I learnt (self-learning) the basics of structure in C today and wrote this simple
i write a simple Rss Feed reader now i have this problem, in the
I wrote this program in C and also in erlang To practice I tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.