Thread: Help - reading a file using array

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Help - reading a file using array

    hi,

    can someone give me the idea on how to read the following file (student's mark text file) using array

    No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT
    1 BK20008 4 3 5 18 18
    2 BK20002 5 5 4 10 8
    3 BK20003 3 4 5 18 10
    4 BK20006 4 3 4 20 14
    5 BK20005 5 4 3 13 17
    6 BK20004 2 5 2 4 16
    7 BK20007 3 4 4 21 14
    8 BK20001 4 3 5 11 15
    9 BK20009 3 2 4 10 10
    10 BK20010 4 3 3 23 18

    i'm not asking for code because this is my project..After reading the file, the program have to calculate the grade and sort them, but first i have to know how to read this file

    any kindly help i will be very appreciate!
    thanks in advance

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    fread(), fgets() or fscanf()
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    1
    where is the problem ? you can read it like any other text file..

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    how would be the design of the code? can give me some example which i can refer to..

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gseed87 View Post
    how would be the design of the code? can give me some example which i can refer to..
    If you cannot find an example of fread(), fgets(), or fscanf() on your computer or the internet, you should give up programming right now.

    At least try to use one of them, and if it does not work, come back and ask "what is wrong"...right now what is wrong is you haven't tried to do anything.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    this is what i written so far..obviously it cannot run, it only can run if i remove first line and the matrix column. please tell me what to do..

    Code:
    #include <stdio.h>
    
    
    void main (void)
    {
    	char matrix[200];
    	int i, j, num_elem, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];
        FILE *infile;
    
    	infile = fopen("studentsmark.txt","r");
    
    	i=1;
    
    	while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no[i],&matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i], &final[i]) !=EOF) i++;
    	
        num_elem = i;
    
    	for(j=1;j<num_elem;j++)
    	{
    	 printf("%d %s %d %d %d %d %d %d\n",no[j], matrix[i], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j], final[j]);
    	}
    
    	fclose(infile);
    
    }

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Doing this directly with fscanf makes debugging tricky, I am sorry I recommended that. What might be better is to use a combination of fgets() and sscanf(), because that will allow you to check the content of your input like this:
    Code:
    int got; // for the return value of sscanf
    char line[4096]; // our buffer
    char matrix[20][200];  // what you had before was wrong -- it was a single string of 200 characters
    
          while (fgets(line,4096,infile)) {           /* no & with the char pointer (matrix[i]) */
                    got=sscanf(line,"%d %s %d %d %d %d %d",
                         &no[i], matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i]);
                    printf("%s\ni %d, got is %d, No is %d proceeding...\n",line,i,got,no[i]); 
                    i++;
            }
    Once you are sure this works properly, you can shorten it to fscanf.

    I removed the &final[i] since in your example input file there are five numbers after the string, not six.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    fread error

    Hello,

    I have also been trying to read data ( float type) from a .txt file. But i have some problems
    Here is the code snippet.
    Code:
    		while(!feof(fp))                       //till end of file
    		{
    			
    			i = fread( temp, sizeof(temp), 1, fp);  temp is a float variable.
                                 
    		}
    I get a compiler error saying ( cannot convert from float to void*.

    The text file has an array of float data.

    1) What could be wrong in this?? ( i also tried type casting..but error remains)

    2) Also, fread reads data sequentiallly right? .i.e element by element in a row and then next row..and so on?

    Thanks.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    fread() isn't going to work like you expect with text files. It's meant for binary data. It doesn't have any concept of rows or elements, just number of bytes to read. If your input file looks like this:

    1254.323523

    And do fread(buf, sizeof(float),1,fp), it's going to read 4 bytes and try to convert the binary representation of those 4 characters ("1254") into a floating point value. The result depends on exactly what machine you're running on, but my guess would be that you'd get either 2.5932651581683785e-9 or 1.6875198127763724e-7.

    The next call to fread would grab ".323" and do the same thing. Like I said, not what you'd expect. If you're trying to read numbers from a file, and can read those numbers using a normal text editor, use fgets()/sscanf() for the input.

    But to answer your question, the first argument of fread() expects a pointer. You're passing in a float. Use the operator that converts from "float" into "pointer to float" and it will compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM