Thread: File I/O with sort

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    File I/O with sort

    Hello,

    How can I read a file with student number, student name, grade and sort it regarding student number, student name or grades and write it to a new file?

    This is not a homework question I have an exam and I am studying and probably something like this will be asked.

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Pseudo C:

    Code:
    fin = open_file();
    array = new_Array();   // we'll store in array and sort.
    i = 0;
    while ( more_record(fin) ) {
       record = get_record(fin);
       store_in_array(array,i,record);          
       ++i;
     }   
       
       qsort();    // pass your comparison function.
       write_to_outfile();

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Make a struct student with all of the fields you want, then make an array of these structs to store the data from the input file. Once that is done, the sorting is simply a matter of passing your array to any sorting algorithm you care to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM