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 8966725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:04:51+00:00 2026-06-15T17:04:51+00:00

So if I have a file with several numbers, and I open the file

  • 0

So if I have a file with several numbers, and I open the file using

fp = fopen (filename, "r");

So now I can read the contents of the file correct? How could I do something with the file. Like in this file there are numbers and I want to be able to add them up.

fscanf(fp) 

would be the beginning of what is supposed to be correct? But I am not sure what to do beyond it. What code represents the items inside the file I am opening? If it is “x” then I want to add all the “x”‘s up and then divide it by the total number of files there are.

How can I use the variables inside a file and do things with them?


Edited code:

if (fp != NULL)
{
     while (fscanf(fp, "%lf", &d) == 1)
     sum += d;
     mean = sum / total;

     printf ("The number of data values read from this file was %.0lf\n", total);
     printf ("\n%.2lf\n", mean);

     fclose(fp);
}

if (fp != NULL)
{
    do
    {
        c = fgetc(fp);
        if (c != EOF)
        {
            if ((char)c == '\n')
            total++;
        }
    } while (feof(fp) == 0);
  • 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-15T17:04:52+00:00Added an answer on June 15, 2026 at 5:04 pm

    If the only content in the file is valid numbers, this will work. Actually, it will work until the first text that isn’t a valid number:

    #include <stdio.h>
    
    int main(void)
    {
        double sum = 0.0;
        char const *filename = "data.file";
        FILE *fp = fopen(filename, "r");
        if (fp != 0)
        {
            double d;
            while (fscanf(fp, "%lf", &d) == 1)
                sum += d;
            fclose(fp);
        }
        printf("%g\n", sum);
        return(0);
    }
    

    As a general rule, if a function opens a file successfully, it should close it when it’s finished. In this example, the system would close the file anyway, but it is as well to get into good habits. With a little more care and attention, you could distinguish between a format error (return value of 0 from fscanf()) and EOF or I/O error.

    • 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 in C to read a file containing several
I have a file which contains several thousand numbers, each on its own line:
I have several file with the following format: lat,lon,value. I would like to plot
I have a file with several column loaded in R. What I want to
I have a CSV file with several entries, and each entry has 2 unix
I have a text file with several different sections. Each section has a header
I have a batch file that runs several python scripts that do table modifications.
I have a file from which I've output several columns of information, 4 to
I have a file attachment class (FileAttachment) which is used in several other classes.
I have an Eclipse plugin project that has several file resources that I would

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.