Thread: Passing data from file to arrays

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Passing data from file to arrays

    I haven't been using files for very long, not sure if im using the correct functions here, but I keep getting 'segmentation fault', and I'm not sure where my problem might be.

    Code:
    #include<stdio.h>
    
    /* Loads data from file into arrays */
            void loadData(int stu[],char name[][20],double t1[],double t2[], double a[], double labs[], double exams[])      
            {
            int i;
            FILE *fp;
    
            fp = fopen("student.txt", "r");
    
            for (i = 0; i < 9; i++){
                    fscanf(fp, "%d,%c,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], t2[i], a[i], labs[i], exams[i]);
                    printf("%d,%c,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], t2[i], a[i], labs[i], exams[i]);
            }
    fclose(fp);
    }
    main ()
    {
            int stu[10];
            double t1[10], t2[10], a[10], labs[10], exams[10];
            char name[10][20];
    
            loadData(stu, name, t1, t2, a, labs, exams);
    }
    Basically I just want to read into a file, and copy each field separated by commas into different arrays. Any help would be greatly appreciated!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    name[i] is a string not a character. You have the wrong flags in your fscanf. %c should be %s
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    I fixed the specified errors, but I'm still getting segmentation fault :S

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You should also be checking if the file was opened successfully (i.e. if fp != NULL )

    Also, main() should be int main(void) and should return 0 at the end of the function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Fixed both of those, still receiving 'segmentation fault'.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you repost the code please?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Code:
    #include<stdio.h>
    
    /* Loads data from file into arrays */
            void loadData(int stu[],char name[][20],double t1[],double t2[], double a[], double labs[], double exams[])
            {
            int i;
            FILE *fp;
    
            fp = fopen("student.txt", "r");
            if (fp == NULL)
                    printf("Cannot open file!");
            else{
                    for (i = 0; i < 9; i++){
                            fscanf(fp, "%d,%s,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], a[i], labs[i], exams[i]);
                            printf("%d,%s,%lf,%lf,%lf,%lf,%lf", stu[i], name[i], t1[i], t2[i], a[i], labs[i], exams[i]);
                            }
            }
            fclose(fp);
    }
    main(void)
    {
            int stu[10];
            double t1[10], t2[10], a[10], labs[10], exams[10];
            char name[10][20];
    
            loadData(stu, name, t1, t2, a, labs, exams);
            return 0;
    }
    There are no compile errors, just this silly segmentation fault. Really frustrated with this at the moment :P

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What is your longest name string in your input file?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Code:
    00101010,80.0,79.0,88.0,67.0,90.0,Bob Newhart
    00111010,88.0,77.0,66.0,57.0,56.0,Alex Trebek
    00121010,80.0,79.0,88.0,67.0,90.0,Stephen Harper
    00131010,88.0,77.0,66.0,57.0,56.0,Alfred Newman
    00141010,80.0,79.0,88.0,67.0,90.0,Vic Firth
    00151010,88.0,77.0,66.0,57.0,56.0,Neo
    00161010,80.0,79.0,88.0,67.0,90.0,Clive Barker
    00171010,88.0,77.0,66.0,57.0,56.0,Clint Eastwood
    00181010,80.0,79.0,88.0,67.0,90.0,George/Fred Weasley
    Wow after pasting this.. I think I found my problem. I could be wrong, but I have the variables out of order on the fscanf. Gonna fix that right now..

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Nvm, even placing the %s at the end didn't fix my problem.

    EDIT: Would arranging the order of variables in the function itself and the call of the function in main make any difference?

    EDIT:EDIT: Doesn't seem like it did D=
    Last edited by Chris5; 12-06-2010 at 10:11 PM.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ahh. The problem is clear now. Scanf stops when it encounters a space. If you want to use spaces then I suggest using fgets() with sscanf(), sprintf(). You can read more about these on the FAQ or using google.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Are you saying to use all three ( fgets(), sscanf() and sprintf() ) to pass those fields into the arrays? For example using fgets for the string, name[i]? And sscanf() for the int and doubles?

    Then of course use sprintf() to print, as opposed to simply using printf? (By the way, whats the advantage here? To my understanding if for example name[0] becomes 'Chris', printf and sprintf will likely do the same thing. Is this correct?)

    Gonna try and fix this now, see if it works. Thanks for your help.
    Last edited by Chris5; 12-06-2010 at 10:54 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by claudiu View Post
    Scanf stops when it encounters a space.
    Technically, %s stops when it encounters a space. You can make scanf not stop on spaces, you just don't use %s.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by quzah View Post
    Technically, %s stops when it encounters a space. You can make scanf not stop on spaces, you just don't use %s.

    Quzah.
    Right, that's what I meant, thanks for the clarification Quzah.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by Chris5 View Post
    Then of course use sprintf() to print, as opposed to simply using printf? (By the way, whats the advantage here? To my understanding if for example name[0] becomes 'Chris', printf and sprintf will likely do the same thing. Is this correct?)
    Blah nevermind the crap about sscanf and sprintf, never even seen these functions yet in my studies. I will read up on them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Storing # from Data File in Arrays?
    By dakarn in forum C Programming
    Replies: 7
    Last Post: 11-13-2008, 09:15 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM