Thread: Help reading data file...

  1. #1
    Unregistered
    Guest

    Question Help reading data file...

    I can't get the program to read the data file. I am supposed to output the five values in the data file five per line using pointers. When I debug it is not reading any of the values in the data file.
    Any tips? Thanks!
    Here's my program:


    #include <stdio.h>
    FILE *myinptr;
    void fiveperline(double *);
    int main()
    {
    int i;
    double x[10];
    myinptr=fopen("data.txt", "r");
    for (i=0;i<10;i++)
    fscanf(myinptr,"%.2lf", &x[i]);
    fclose(myinptr);
    fiveperline(x);
    return 0;
    }


    void fiveperline(double *x)
    {
    int i;
    for (i=0;i<10;i++){
    printf("%.2f\t", x[i]);
    if((i+1)%5==0)
    printf("\n");
    }

    }

    Data File (data.txt)
    8.50
    8.60
    8.90
    7.95
    8.89
    6.95
    7.25
    8.76
    9.45
    8.25

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Change this
    fscanf(myinptr,"%.2lf", &x[i]);

    To this
    fscanf(myinptr,"%lf", &x[i]);

    Seems to work then

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Talking

    I forgot to log in when I posted the topic. It's working. Thanks!
    Last edited by illmatic; 02-24-2002 at 06:13 PM.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    3
    edit:got it to work, but how would I send it he mean value back to the main using a pointer and not a return statement? THanks!

    #include <stdio.h>
    FILE *myinptr;
    void fiveperline();
    void mean(double *);
    void main()
    {
    int i;
    double x[10];
    myinptr=fopen("data.txt", "r");
    for (i=0;i<10;i++)
    fscanf(myinptr,"%lf", &x[i]);
    fclose(myinptr);
    fiveperline();
    mean(x);
    }

    void mean(double *myinptr)
    {
    int k;
    double mean=0;

    for (k=0;k<10;k++){
    mean+=*(myinptr+k);
    }
    mean=mean/10;
    printf("mean=%.2lf", mean);
    }
    Last edited by illmatic; 02-24-2002 at 07:36 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does no one read anymore?

    Code:
    #include <stdio.h> 
    FILE *myinptr; 
    void fiveperline(); 
    void mean(double *); 
    int main() // YES, int
    { 
    int i; 
    double x[10]; 
    double meanval;
    myinptr=fopen("data.txt", "r"); 
    for (i=0;i<10;i++) 
    fscanf(myinptr,"%lf", &x[i]); 
    fclose(myinptr); 
    fiveperline(); 
    mean(x,&meanval); 
    printf("mean=%.2lf", meanval); 
    } 
    
    void mean(double *myinptr, double *meanptr) 
    { 
    int k; 
    double mean=0; 
    
    for (k=0;k<10;k++){ 
    mean+=*(myinptr+k); 
    } 
       *meanptr=mean/10; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM