Thread: Sort Program

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Sort Program

    I have to make a program for my topics class that is a "poor mans" variation to the unix sort command. Program is suppose to read a filename in as a command line argument. We are suppose to use the malloc() function in this program. also a requirement is :

    Re-implement the readLine() function so that it no longer makes use of fgets(). Instead, process the input on a given line character-by-character. I can post the code of this readLine() function also. I'm really confused on where to go from here, and also how I am even suppose to rewrite this readLine() function w/o the fgets(). Heres my code so far, and the function were suppose to change:

    readLine():
    Code:
    /* Read and store one line of input
    
    
       return NULL if stream is at eof or there was a read error
       otherwise returns the pointer s to the char array
    
    
       reads and stores up to n characters in the char array, stopping at newline
       if there isn't space to hold the entire line, overflow is set to true
       and any trailing characters on that line are discarded
    */
    char *readLine(char *s, int n, FILE *fp, bool *overflow) {
        char *result;
        int length;
        int ch;
        int skipped;     /* the number of NON-newline characters skipped over
                            for lines too long to fit in the given char array */
        
        result = fgets(s, n, fp);   /* read a line */
        if (result == NULL) {
            *overflow = false;
            return NULL;    /* either stream is at eof or there was a read error */
        }
        
        /* read was successful:  did we get the entire line? */
        length = strlen(s);
        if (s[length - 1] == '\n') {
            s[length - 1] = '\0';    /* we don't want to store the newline */
            *overflow = false;
        }
        else {
            /* skip to end of current line */
            skipped = 0;
            while ((ch = getc(fp)) != '\n' && (ch != EOF))
                skipped++;
            *overflow = (skipped > 0);
        }
        return s;
    }
    My Code so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_LINES 1000   /* maximum number of reminders */
    #define WORD_LENGTH 10      /* max length of reminder message */
    
    
    int read_line(char str[], int n);
    
    
    int main(int argc, char *argv[])
    {
      char *lines[MAX_LINES];
      char ch, line = argv[1];
      int day, i, j, num_lines = 0;
    
    
      for (;;) {
        if (num_lines == MAX_LINES) {
          printf("-- No space left --\n");
          break;
        }
    
    
        printf("Enter name of file.");
        FILE *fp = fopen(argv[1], "r");

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you can replace this
    result = fgets(s, n, fp); /* read a line */

    With a loop which looks a lot like this (which you already have later)
    while ((ch = getc(fp)) != '\n' && (ch != EOF))

    The only real trick is adding the counter to make sure you don't overflow the buffer, and making sure the resulting buffer always ends in \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with sort program
    By leroyjenkens in forum C Programming
    Replies: 8
    Last Post: 06-20-2012, 02:03 PM
  2. Word Sort Program
    By daggerhunt in forum C Programming
    Replies: 9
    Last Post: 03-20-2012, 01:01 PM
  3. quick sort program
    By rakeshkool27 in forum C Programming
    Replies: 7
    Last Post: 05-21-2010, 02:29 AM
  4. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 10:51 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM