Thread: how can i read from a file in this manner?

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

    how can i read from a file in this manner?

    if the arrangement of every student account in the file is like this...

    STUDENT NUMBER \t NAME \t BIRTHDATE \t COURSE
    i.e...

    stud_num0 name0 bday0 course0
    stud_num1 name1 bday1 course1
    stud_num2 name2 bday2 course2
    how can i transfer these info accordingly to an array of struct like this?...

    Code:
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    
    ....
    
    STUDENT array[20];
    i have something like...

    Code:
    void read_from_file(FILE *file, STUDENT array[20])
    {
    	int i;
    
    	file = fopen("Students.txt", "r");
    
    	for(i = 0; !feof(file) && i < 20; i++)
    	{
    		fscanf(file, "%lld %s %s %s", &array[i].stud_num, array[i].name, array[i].bday, array[i].course);
    	}
    
    	fclose(file);
    }
    thanks in advance!
    Last edited by Huskar; 03-31-2009 at 09:42 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you passing a file handle instead of declaring it locally?
    Why don't you check the return values of fscanf and fopen?
    What specifically are you having problems with?


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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    UPDATE:

    ive made my code work ONLY when i enter info that doesnt contain spaces...
    i can make the read_from_file and write_to_file work when my strings have no spaces...

    how can i read strings with spaces from a file?
    Last edited by Huskar; 03-31-2009 at 09:44 PM.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Huskar View Post
    UPDATE:

    ive made my code work ONLY when i enter info that doesnt contain spaces...
    i can make the read_from_file and write_to_file work when my strings have no spaces...

    how can i read strings with spaces from a file?
    I don't understand how your asking these questions if your working with files, shouldn't you have read up on fread() and fwrite()already? You should check these out and do some more reading on structures is my suggestion. then tackle your problem again.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    so should i? sorry im very new to this and im in a hurry...

    Code:
    void read_from_file(FILE *file, STUDENT array[20])
    {
    	int i;
    
    	file = fopen("Students.txt", "r");
    
    	for(i = 0; !feof(file) && i < 20; i++)
    	{
    		fscanf(file, "%lld", &array[i].stud_num);
                    fread here?  then save to array[i].name
                    fread here? then save to array[i].bday
                    fread here? then save to array[i].course?
             }
    
    	fclose(file);
    }
    is this correct?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've noted a few things in blue.



    STUDENT NUMBER \t NAME \t BIRTHDATE \t COURSE
    i.e...

    Actually, just one space between data would be fine:
    e.g.: 2154 Joe Student 12/11/1982 Mathematics


    how can i transfer these info accordingly to an array of struct like this?...

    Code:
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    
    ....
    
    STUDENT array[20];
    i have something like...

    Code:
    void read_from_file(FILE *file, STUDENT array[20])
    {
    	int i;
    
    
    	if((file = fopen("Students.txt", "rt" == NULL)  {
               printf("\n File \"Students.txt\" didn't open - exiting program\n");
               exit(1);
            }
    
    	/*if you're reading in a data file, it's important to read it *all*, so a fixed number
               for loop is not what I'd recommend. Use a do while loop instead.
            */
            MaxStudents = 0;
            while((fscanf(file, "%lld %s %s %s", &array[i].stud_num, array[i].name, array[i].bday, array[i].course) != EOF) {
                if(array[i].stud_num > 0)
                    MaxStudents++;
    	}
    
           
    }
    I added the 't' to the 'r' file mode, because not all systems are set to open files by default, in text mode, which is what you want, here. Mine for instance, opens files in default r or w mode, as binary.

    There is a good way to handle this using fread and fwrite, but carry on just as you are doing, Huskar. You need to learn *both* text and binary reads and writes, and you're already well on your way with text reading and writing, with this program.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Huskar View Post
    so should i? sorry im very new to this and im in a hurry...

    Code:
    void read_from_file(FILE *file, STUDENT array[20])
    {
    	int i;
    
    	file = fopen("Students.txt", "r");
    
    	for(i = 0; !feof(file) && i < 20; i++)
    	{
    		fscanf(file, "%lld", &array[i].stud_num);
                    fread here?  then save to array[i].name
                    fread here? then save to array[i].bday
                    fread here? then save to array[i].course?
             }
    
    	fclose(file);
    }
    is this correct?
    sorry, if you're too much in a hurry to learn the language. let me know when you have time, I'll be right over here on the edge of my seat.

    Code:
    if((file = fopen("Students.txt", "rt" == NULL)  {
               printf("\n File \"Students.txt\" didn't open - exiting program\n");
               exit(1);
            }
    actually its more like
    Code:
    if ( (file = fopen("students.txt", "r") ) == NULL) {
    also you don't need to add a 't' token there. because
    Code:
    fopen(const char *filename, "r")
    opens a filename for reading in text mode by default anyway. in fact, in place of the 't' why don't you put any letter from a - z (except b) and tell me what happens.
    Last edited by caroundw5h; 03-31-2009 at 10:48 PM. Reason: fopen operations and usage corrections
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM