GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jun-2009, 21:53
alishaikhji alishaikhji is offline
New Member
 
Join Date: Jun 2009
Posts: 3
alishaikhji is on a distinguished road

Getting float data from text file to array


Hello

I am a newbie to C language. I need to use the data (48 values) from a text file (which area float values) in the following form:
80.00 23.00 82.00 22.00 80.00 23.00 81.00 24.00 80.00 22.00 80.00 23.00 84.00 24.00 80.00 22.00 82.00 32.00 120.00 102.00 120.00 132.00 120.00 140.00 120.00 150.00 110.00 141.00 120.00 128.00 110.00 118.00 105.00 100.00 105.00 87.00 80.00 38.00 82.00 30.00 80.00 23.00 78.00 22.00 80.00 22.00 79.00 22.00

I need this data to form an array which i could use for further work. I have worked out the following program but it gives 0.000000 (48 times) only.

I am using CodeBlocks open source IDE/compiler on windows XP.

My present program is as follows:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

 FILE *fp;   /* file pointer */
 float mgone[48];
 int i;

if((fp=fopen("mg1.txt", "r"))==NULL) {
    printf("Cannot open file.\n");
  }

  if(fread(mgone, sizeof(float), 48, fp) != 48) {
    if(feof(fp))
       printf("Premature end of file.");
    else
       printf("File read error.");
  }
  fclose(fp);

  for(i=0; i<48; i++)
    printf("%f ", mgone[i]);

  return 0;
}

Thanks & Regards

Ali
Last edited by LuciWiz : 02-Jun-2009 at 02:28. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 01-Jun-2009, 23:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Getting float data from text file to array


Quote:
Originally Posted by alishaikhji
...data (48 values) from a text file...to form an array...

The fread() function reads binary values, (bytes that make up floating point numbers in machine representation).

To read the values that are expressed as characters in a text file, you can make a loop that reads the values with fscanf().


Maybe do something like:
CPP / C++ / C Code:
.
.
.
    int i, n;
    FILE *fp;
    float mgone[48];
.
.    /* open the file */
.
    for (i = 0; i < 48; i++) {
        if (fscanf(fp, "%f", &mgone[i]) != 1) {
            break;
        }
    }
    fclose(fp);
    n = i;
    printf("Number read = %d\n", n);

    for (i = 0; i < n; i++) {
        printf("mgone[%d] = %f\n", i, mgone[i]);
    }

Regards,

Dave
  #3  
Old 02-Jun-2009, 20:03
alishaikhji alishaikhji is offline
New Member
 
Join Date: Jun 2009
Posts: 3
alishaikhji is on a distinguished road

Re: Getting float data from text file to array


thanks Dave.. it solved the problem
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 60 20-Aug-2009 06:04
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 111 19-Feb-2009 04:54
Canny edge detector Skm Java Forum 1 01-Jan-2006 07:51
How to read unknown total of int data from text file to a 2-dim array in C or C++? ladyscarlet99 C Programming Language 2 09-Sep-2005 15:07
How to search a huge text file for data? JdS MySQL / PHP Forum 7 27-May-2003 10:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.