Thread: Problem reading/printing Data

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    30

    Problem reading/printing Data

    I wrote following code for reading Data and storing it on an array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MAX_L 100
    
    
    
    
    int read(double *arr, int size, char *filename);
    int read(double *arr, int size, char *filename)
    {
    int z;
    FILE *infile;
    infile = fopen(filename,"r");
    
    
    if (infile==NULL)
        {perror("Error");
        return 0;
        }
    
    
    for(z=0;;z++)
        {
        if(fscanf(infile,"%lf",&(arr[z]))==EOF || z>MAX_L)break;
        
        }
    return z;
    }
    
    
    //void Print(double *arr, int length);
    void Print(double *arr, int length)
    {
    int i;
    for(i = 0;i<length;i++)
        {
        printf("A[%d]: %d\n",i,arr[i]);
        }
    }
    
    
    
    
    int main()
    {
    double f[MAX_L];
    int r;
    r = read(f, MAX_L, "Points.dat");
    Print(f,lesen);
    }
    It compiles without problems, however it only prints zeros. Can anybody give me a hint what the problem might could be?
    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It compiles without problems, however it only prints zeros.
    Perhaps you should increase your compiler warning level and fix all warnings and errors?

    main.c||In function ‘read’:|
    main.c|11|warning: unused parameter ‘size’ [-Wunused-parameter]|
    main.c|36|warning: no previous declaration for ‘Print’ [-Wmissing-declarations]|
    main.c||In function ‘Print’:|
    main.c|42|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat=]|
    main.c||In function ‘main’:|
    main.c|54|error: ‘lesen’ undeclared (first use in this function)|
    main.c|54|note: each undeclared identifier is reported only once for each function it appears in|
    main.c|52|warning: variable ‘r’ set but not used [-Wunused-but-set-variable]|

  3. #3
    Guest
    Guest
    Line #38
    Code:
    printf("A[%d]: %d\n",i,arr[i]); // You tell printf to expect two integers, but arr[] contains doubles
    You should really enable compiler warnings and learn to indent your code properly.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    Thank you very much! Lien #38 was the problem. Can you pleas tell me how to increas my compiler warning level?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What compiler/IDE are you using?

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    gcc (GCC) 5.3.0

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    are you even adding
    Code:
     -Wall
    to the line?

    here are a few more

    Using the GNU Compiler Collection (GCC): Warning Options

  8. #8
    Guest
    Guest
    Quote Originally Posted by Gamma_123 View Post
    gcc (GCC) 5.3.0
    I recommend adding the flags -Wall -Wextra -pedantic for good coverage, especially while you're still learning.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    How can I enable these flags? By typing in -Wall, I get the error that the comand is either wrong spelled or couldn't be found.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Gamma_123 View Post
    How can I enable these flags? By typing in -Wall, I get the error that the comand is either wrong spelled or couldn't be found.
    how are you compiling your program, command line or IDE?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a file and printing it
    By Linux Trojan in forum C Programming
    Replies: 27
    Last Post: 07-02-2011, 12:45 PM
  2. Reading in a file and printing out a graph from the data
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 07:32 PM
  3. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 07:02 AM
  4. Database::problem printing last data twice.
    By vaibhav in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2006, 04:31 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

Tags for this Thread