Thread: Storing data from text file into array?

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

    Storing data from text file into array?

    Hi, I'm having trouble storing information from a text file into an array. I have the code working fine if it stores into non-array variables, but as soon as I try to use an array the program stalls. Can somebody point out the issue?

    Code that works without array
    Code:
    	FILE *fp = fopen("students.txt", "r");
    	int stu;
    	char name[30];
    	double t1, t2, a, labs, exams;
    	
    	fscanf(fp, "%d,%lf,%lf,%lf,%lf,%lf,%[A-Za-z /]", &stu, &t1, &t2, &a, &labs, &exams, name);
    	printf("%d,%s,%lf,%lf,%lf,%lf,%lf", stu, name, t1, t2, a, labs, exams);
    Code that fails with array
    Code:
    	FILE *fp;
    	fp = fopen("students.txt", "r");
    	int stu[MAX_SIZE], i = 0;
    	char name[MAX_SIZE][20];
    	double t1[MAX_SIZE], t2[MAX_SIZE], a[MAX_SIZE],
    	labs[MAX_SIZE], exams[MAX_SIZE];
    	
    
    	fscanf(fp, "%d,%lf,%lf,%lf,%lf,%lf,%[A-Za-z /]", stu[i], t1[i], t2[i], a[i], labs[i], exams[i], name[i]);
    	printf("%d %s %lf %lf %lf %lf %lf", stu[i], name[i], stu[i], t1[i], t2[i], labs[i], exams[i]);
    The file is structured in this format:
    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    fscanf(fp, "%d,%lf,%lf,%lf,%lf,%lf,%s",&stu[i], &t1[i],&t2[i],&a[i], &labs[i],&exams[i], name[i]);
    You forgot something. Also, compile with warnings on.


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

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Thought that the ampersand was omitted for arrays, forgot that only applies to strings.

    Thanks a bunch.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check the return value of fscanf.
    Code:
    for( x = 0; x < arrayelements; x++ )
    {
        if( fscanf( ... ) != numberexpected && feof( ... ) )
            break;
    }
    Something like that would be fine. Optionally, you could add the colored condition there too.

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

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Yeah, found that right before you posted haha.

    Once last question :P

    I've completed the program and it compiles and runs fine in Windows with MinGW. When I try to compile it through my school's Linux cluster, it compiles fine but I get a Segmentation Fault whenever I try to run it. Any idea?

    Please do tell if you need to see the code or not. My school has a pretty strict policy and I don't want to get kicked out 2 semesters down the road out of some slim chance they see that I posted my program online :P
    Last edited by flaris; 12-02-2010 at 01:16 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    See if it dumps a .core file, and debug it with gdb. It should help you find out what function it's in when it crashes.


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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by flaris View Post
    Thought that the ampersand was omitted for arrays, forgot that only applies to strings.

    Thanks a bunch.
    Ampersand IS omitted for arrays since an array name by itself is already an address of sorts. This applies whether it's a char array, int array, double array, whatever.
    But in the fscanf() case, you are referencing array[i] - a specific element. Not an address. So you need to use the ampersand after all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM