LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2009, 09:53 AM   #1
wsduvall
Member
 
Registered: Aug 2006
Posts: 92

Rep: Reputation: 16
Read floats from file


Hey guys.I've been working on this for quite some time, and I feel like I shouldn't be having this much trouble. I have a list of newline separated floats. The following code will read a float from a file "myfile.txt"

Code:
#include <stdio.h>

int main ()
{
  float f;
  FILE *fp;

  fp = fopen ("myfile.txt","r");
  fscanf (fp, "%f", &f);
  fclose (fp);
  printf ("I have read: %f\n",f);
  return 0;
}
What I cannot figure out how to do is make a loop which will read the list of floats into an array. Any help would be greatly appreciated!

Last edited by wsduvall; 03-10-2009 at 09:54 AM.
 
Old 03-10-2009, 10:10 AM   #2
wsduvall
Member
 
Registered: Aug 2006
Posts: 92

Original Poster
Rep: Reputation: 16
Looks like I figured it out on my own. This code works:

Code:
#include <stdio.h>

int main ()
{
  int i;
  float f[100];
  FILE *fp;

  fp = fopen ("myfile.txt","r");
  for( i = 0; i < 50; i++ ) {
    if(fscanf (fp, "%f\n", &f[i]) !=1) {
      break;
    }
  }
  fclose (fp);
  printf ("I have read: %f and %f and %f\n",f[0],f[1],f[2]);
  return 0;
}
But there is one strange problem.

Code:
[wsduvall@Asar mpi]$ ./a.out 
I have read: 3.141600 and 23.120001 and 12.143000
[wsduvall@Asar mpi]$ cat myfile.txt
3.141600
23.12
12.143
Notice the difference between what the program is reading and what is actually in the file? It might have something to do with the precision... any ideas?
 
Old 03-10-2009, 10:18 AM   #3
wsduvall
Member
 
Registered: Aug 2006
Posts: 92

Original Poster
Rep: Reputation: 16
And I fixed that problem too... just changed float to double and all the %f's to %lf. Its only a shame I can't thank myself...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Only File System? Can't read superblock! haughjd Linux - Software 4 01-15-2008 08:27 AM
floats in c++ ArlexBee-871RBO Programming 2 03-02-2007 08:08 PM
C++ floats and doubles ArlexBee-871RBO Programming 0 03-01-2007 09:04 PM
how do I make a read-only file NOT read-only? robster Linux - General 1 02-17-2004 09:11 PM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration