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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:52:04+00:00 2026-06-18T01:52:04+00:00

—This is a Homework question— I’m having problems reading float values from a text

  • 0

—This is a Homework question—

I’m having problems reading float values from a text file using fscanf.

Basically I’m trying to read float values from a file and store them in a dynamic array.
The input file has two floats per line. so a line might be “0.85 7.34” (w/o quotes). so I’m trying to use fscanf(fp, “%f %f”, &coordinates[i], &coordinates[i++]) to read in the 2 float values. when i print it shows as 0.00000. Below is the code I wrote and the output it produces.

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

int main (int argc, char *argv []) {

FILE * fp = fopen("nums", "r");

float *coordinates;
float *tmp;
int i = 0;
int ARRAY_SIZE = 5;
coordinates = malloc(5*sizeof(float));

while (fscanf(fp,"%f %f", &coordinates[i], &coordinates[i++]) > 1)
{

  printf("iteration# %d | coord1 = %f coord2 = %f \n", i, &coordinates[i-1], &coordinates[i]);

  if (i >= ARRAY_SIZE)
  {
    tmp = realloc(coordinates, (i*2)*sizeof(float));
    coordinates = tmp;
    ARRAY_SIZE = i*2;
  }
  i++;
}

for(i = 0; i < 8; i++)
  printf("%f\n", &coordinates[i]);


return 0;
}

OUTPUT:

iteration# 1 | coord1 = 0.000000 coord2 = 0.000000 
iteration# 3 | coord1 = 0.000000 coord2 = 0.000000 
iteration# 5 | coord1 = 0.000000 coord2 = 0.000000 
iteration# 7 | coord1 = 0.000000 coord2 = 0.000000 
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000  
  • 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-18T01:52:05+00:00Added an answer on June 18, 2026 at 1:52 am

    You don’t use the “address of”-operator & with printf. fscanf requires a pointer to the data so it knows can change the variables value, while printf does not.

    Change:

    printf("iteration# %d | coord1 = %f coord2 = %f \n", i, &coordinates[i-1], &coordinates[i]);
    

    To:

    printf("iteration# %d | coord1 = %f coord2 = %f \n", i, coordinates[i-1], coordinates[i]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm including a local class that requests a file from a remote server. This
I'm currently working on a little harvester, using this dataset of 2700 foundations .
I'm using some of Firefox's specially-defined values for cursor, in particular -moz-zoom-in -moz-zoom-out -moz-grab
We're building an app, our first using Rails 3, and we're having to build
I'm using a DatePicker ( org.apache.wicket.extensions.yui.calendar.DatePicker — Javadoc ) in a form. The form
What is the absolute fastest way to read and write strings from a file
This is a follow-up to the question: Should the folders in a solution match
My app uses a WebRequest at certain points to get pages from itself. This
I have read and added a question to this thread http://code.google.com/p/fullcalendar/issues/detail?id=143 but its a
I have the following file: BTA Pos KLD 4 79.7011 5.7711028907 4 79.6231 5.7083918219

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.