Reading data from file

This is a discussion on Reading data from file within the C Programming forums, part of the General Programming Boards category; I must be making a simple mistake but I can't seem to find out what I'm doing wrong. Here is ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Reading data from file

    I must be making a simple mistake but I can't seem to find out what I'm doing wrong. Here is my problem: I'm reading data from a file into an array, but the values are only being written into the first cell of the array - the array is not incrementing like I think it should. The print statement is working an prints the contents of the file correctly but the "data array" is not filling up.

    Code:
     char fileName[100];
        int  data[100];
        FILE* sp;
    
     sp = fopen(fileName,"r");
        
        while(k=(fscanf(sp,"%i",data) == 1)){
            printf("%10i\n",*data);
            b += k++ ;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is today "leave off the ampersands on scanf arguments" day? Presumably you don't want to fscanf into data, but some particular slot in the data array.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Quote Originally Posted by tabstop View Post
    Is today "leave off the ampersands on scanf arguments" day? Presumably you don't want to fscanf into data, but some particular slot in the data array.
    Oh boy, I thought that an array's name is a pointer to the first element in the array and that there is no need to place an ampersand in the fscanf function because of this. What am I not understanding?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is a pointer to the first element in the array, yes. And you are complaining that each element goes into the first element of the array....

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Code:
     char fileName[100];
        int  data[100];
        FILE* sp;
    
     sp = fopen(fileName,"r");
        
        while(k=(fscanf(sp,"%i",data) == 1)){
            printf("%10i\n",*data);
            b += k++ ;
    Are you saying that
    Code:
     while(k=(fscanf(sp,"%i",data) == 1))
    should be changed to
    Code:
    while(k=(fscanf(sp,"%i",data[i]) == 1)
    where i is a loop counter in a for loop?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Once you tack that ampersand on, that would be an excellent idea.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Ok, this is still not working. I've tried everything I can think of but still it's not working as it should. If I run the code I can print the contents of the file just fine,however, they are not being stored in the array. When I pass the array to the function stats the array is not being sorted.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int stats(int data[], int b);
    int main(void){
        char fileName[100];
        int  data[100];
        FILE* sp;
        int k = 0;
        int b=0;
        int i;
        
     
        printf("Enter File :  ");
        scanf("&#37;s",fileName);
        printf("%s\n",fileName);
        sp = fopen(fileName,"r");
        for(i= 0; i<10; i++){
        while(k=(fscanf(sp,"%i",&data[i]) == 1)){array still not filling up
            printf("%10i\n",*data);
            b += k++ ;// b is number of  values read into data[]
        }
        }
        for(i=0;i<10;i++)
            printf("%i",data[i]);
        fclose(sp);
        stats(data,b);
        return 0;
    }
    int stats(int data[],int b){
        int i,j,temp;
       
        for(i=0;i<100;i++){
            for(j=0;j<100;j++){
                if(data[0] > data[1]){
                    temp = data[0];
                    data[0] = data[1];
                    data[1] = temp;
                }
            }
        
      for(i = 0; i< b; i++)
          printf("%i\n",data[i]);
      return 0; 
    }
    Last edited by mesmer; 11-12-2008 at 09:19 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        for(i= 0; i<10; i++){
        while(k=(fscanf(sp,"%i",&data[i]) == 1)){array still not filling up
            printf("%10i\n",*data);
            b += k++ ;// b is number of  values read into data[]
        }
        }
    So, do ten times:
    OK = true;
    while (OK)
    scanf an integer from the file into data[i].
    if scanf faile OK = false;

    Perhaps you'd want to re-write your loop such that you EITHER read 10 times, or that you increment the index when you read an integer successfully.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Perhaps you'd want to re-write your loop such that you EITHER read 10 times, or that you increment the index when you read an integer successfully.
    I thought that the for loop in my code is incrementing or indexing though the data array

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except for the whole "the while loop is inside the for loop, so the for loop doesn't happen" thing.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Except for the whole "the while loop is inside the for loop, so the for loop doesn't happen" thing.
    Actually, I'd say that the for loop does happen - but only after the while-loop is finished, at which point you have either reached the end of the file or some rubbish that you can't get past with fscanf() (e.g. "abc" in the file instead of "132" - fscanf will not "pass" anything that isn't numbers or whitespaces [newlines, spaces, tabs] when reading in numbers).

    So whilst the for-loop does indeed execute 10 times, the numbers read from the file all go into one location.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I meant that (during the read) the while loop is spinning and the for loop isn't. It isn't until all the numbers are read in that the while loop stops and the for loop increments things, and then as you say, there's nothing (useful) left to read in.

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, 08: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, 05: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, 02:58 PM

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