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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    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