Like Tree1Likes
  • 1 Post By Matticus

Trying to read data from an array. (help) (short code)

This is a discussion on Trying to read data from an array. (help) (short code) within the C Programming forums, part of the General Programming Boards category; Code: void read_file(FILE *fp,double arr[][5]); //book says to omit the row array, so thats why its [ ] int main(int ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    Trying to read data from an array. (help) (short code)

    Code:
     void read_file(FILE *fp,double arr[][5]); //book says to omit the row array, so thats why its [ ]
    int main(int argc, char* argv[])
       {
      double arr[6][5];
      FILE *fp;
      fp=fopen(argv[1],"r");
    
    void read_file(FILE*fp,double arr[][5])
      {
      int i=0;
      int j=0;
      while(fscanf(fp,"%lf%lf%lf%lf%lf",&arr[i][j])!=EOF)
      {
      i++;
      j++;
      }
    Error: Line: 12 warning: format '%lf' expects a matching 'double *' argument [-Wformat]


    I dont know what this means and how can i fix it?

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    &arr[i][j] this is only one double, you have 6 doubles in your fscanf.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Quote Originally Posted by camel-man View Post
    &arr[i][j] this is only one double, you have 6 doubles in your fscanf.
    but the file im reading from has 6 numbers across the row

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    You will have to read one double across each column, then move to the next row.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,434
    You're scanning for six values, but only providing one location for them. If you're reading input with a loop, you likely should only be reading in one value for each iteration. And in that case, the index variables ('i' and 'j') wouldn't be incremented at the same time.
    camel-man likes this.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    Also, it appears that you have a curly brace mismatch.

    If you indent your code properly, you will see it quickly.

    "Allman" Indentation (My prefered style)
    Code:
    int main(void)
    {
        ....
        if (banana != delicious)
        {
            if (apple != delicious)
            {
                if (pear != delicious)
                {
                    you = fussy;
                }
            }
        }
    
    
        return 0;
    }

    For more, see Indent style - Wikipedia, the free encyclopedia
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2012, 04:42 AM
  2. Help! What's wrong with my code? How do I read data from a file?
    By adonaldson15590 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2011, 05:07 PM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  5. how do i read this data from a file instead of this array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-16-2002, 11:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21