C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 28th, 2013, 09:19 PM
clmoore3 clmoore3 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 clmoore3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 16 sec
Reputation Power: 0
Sort Program Help

Hi, needing help with c programming. Im suppose to make a "poor mans" variation to the Sort function built into unix. The program is suppose to read in a file and sort the contents of the file. So its a variation of the Unix Sort feature. I have remade the readLine function we were provided so that it doesnt use fgets. I need help on where to go from here, Not sure on how to make a sort function. Here are the reqirements of the program:

Code:
• 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. • Provide code which will create a data structure similar to argv to hold all of the words to be sorted. Use malloc() to ensure that each entry has just the required number of bytes needed to store the words. The final entry in your array should be the NULL pointer. • Implement a sort() function which will rearrange the words in sorted order. To swap two words in your array, note that only a pair of pointers need to move. The strings themselves, once established, will never move.



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 line[WORD_LENGTH + 1];     bool lineOverflow;   /* not enough room in char array? */     int i, j, num_lines = 0;      fp = fopen(FILE_NAME, "r");     if (fp == NULL) {         fprintf(stderr, "Error: can't open %s for reading.\n", FILE_NAME);         exit(EXIT_FAILURE);     }      FILE *fp = fopen(argv[1], "r");     if (fp == NULL) { 	fprintf(stderr, "Error: can't open %s for reading.\n", argv[1]); 	exit(EXIT_FAILURE);     }      while(readLine(line, sizeof(line), fp, &lineOverflow) != NULL) { 	printf("%s", line);     }          fclose(fp);    } return(0); }      char *readLine(char *s, int n, FILE *fp, bool *overflow) {      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 */     int i = 0;      ch = getc(fp);   /* read a line */     if (ch == EOF) {       *overflow = false;       return NULL;    /* either stream is at eof or there was a read error */     }          while(ch != '\n' && i < (n - 1)) {       s[i++] = ch;       ch = getc(fp);     }          /* 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; }   void Sort() {             }

Reply With Quote
  #2  
Old February 28th, 2013, 10:56 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
__________________
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sort Program Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap