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

  • Home
  • SEARCH
  • 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 8121425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:29:05+00:00 2026-06-06T05:29:05+00:00

i have to write a program in C to read a file containing several

  • 0

i have to write a program in C to read a file containing several line of text, each line contains two variables: a number (%f) and a string:

EX: file.txt
============
24.0 Torino
26.0 Milano
27.2 Milano
26.0 Torino
28.0 Torino
29.4 Milano

There is my code:

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

int main (int argc, char *argv[])
{
    int r, line = 0, found = 0;
    float temp, t_tot = 0;
    char loc[32];


    FILE *fp;

    fp = fopen(argv[1], "r");

    if (fp == NULL)
    {
        printf ("Error opening the file\n\n'");
        exit(EXIT_FAILURE);
    }

    if (argc == 3)
    {
        r = fscanf(fp, "%f %s\n", &temp, loc);

        while (r != EOF)
        {
            line++;

            if (r == 2)
            {
                if(strcmp(argv[2], loc) == 0)
                {
                    t_tot += temp;
                    found++;
                }
            }
            else
                printf ("Error, line %d in wrong format!\n\n", line);
        }

        printf ("The average temperature in %s is: %.1f\n\n", argv[2], (t_tot/found);
    }

}

The program needs to read all the line and find the city i wrote on argv[2]. Than it will tell me the average temperature on that city, notifying me if a line in the file is in a wrong format.

The program is compiling correctly to me but it not output anything on screen… how can i solve that? Is it correct to use fscanf on this case or it’s better fgets?

I’m a student so, please, give me an “academical” way to resolve it 🙂

  • 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-06T05:29:07+00:00Added an answer on June 6, 2026 at 5:29 am

    A couple of things.

    First, you gotta use fclose().
    Second, your code needs to fscan() each line in the file. Not just before the while() loop, but within each while loop you gotta fscan() for the next iteration.
    Third, you weren’t figuring the average temperature, you’re figuring the sum of all the tempuratures found. Fix that by changing “t_tot” to “(t_tot / found)” in your last printf().

    Lastly, I’m not sure why you’re not getting any output. Your input is like “myprogram file.txt Milano” right? Works for me. Anyway, here’s your (edited) code back:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
        int r, line = 0, found = 0;
        float temp, t_tot = 0;
        char loc[32];
    
        FILE *fp;
        fp = fopen(argv[1], "r");
    
        if (fp == NULL)
        {
            printf ("Error opening the file\n\n'");
            exit(EXIT_FAILURE);
        } else {
    
            if (argc == 3)
            {
                r = fscanf(fp, "%f %s\n", &temp, loc);
                while (r != EOF)
                {
                    line++;
                    if (r == 2)
                    {
                        if(strcmp(argv[2], loc) == 0)
                        {
                            t_tot += temp;
                            found++;
                        }
                    }
                    else
                        printf ("Error, line %d in wrong format!\n\n", line);
                    r = fscanf(fp, "%f %s\n", &temp, loc);
                }
                printf ("The average temperature in %s is: %.1f\n\n", argv[2], (t_tot / found));
            }
    
        fclose(fp);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program that read from a file that contains the
So I have the following read and write file program for an Array. How
I have a text file named test.txt I want to write a C program
For a class I have to write a program to read in a text
Hi I have a text file containing two arrays and one value(all integers) like
I have to write a program to check if any random string is present
I wanted to write a program that test if two files are duplicates (have
I have a file with millions of URLs/IPs and have to write a program
How can I read a file and convert each line into a date? Ok
i have multiple files each containing 8/9 columns. for a single file : I

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.