Thread: unix fold utility

  1. #16
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    Before handing over this assignment the teacher must have covered how to do option processing in class. Hard to believe that she or he would just pull it out of the hat.
    Why? I'm not a computer science prof yet, but it seems like something where you should be able to spot the angles after a few months. I wouldn't want my students to just sleep thru their final project -- I'd have such a big hat they'd love me forever
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    Quote Originally Posted by MK27 View Post
    I believe this to be a foolproof and completely optimized way of parsing your particular options:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void wrong () {
    	puts("Usage: fold [-s] [-w width] file ...");
    	exit (-2);
    }
    
    int parse_w (char *arg) {
    	int i, retv;
    	if (sscanf(arg,"%d",&retv)!=1) wrong();
    	return retv;	
    }
    
    int main (int argc, char *argv[]) {
    	int i, len, sflag=0, width=80;
    	FILE *fstRO;
    	for (i=1;i<argc;i++) {
    		if (strcmp(argv[i],"-s")==0) {sflag=1;continue;}
    		if (strncmp(argv[i],"-w",2)==0) {
    			if (strlen(argv[i])>2) {
    				if (sscanf(argv[i], "-w%d",&width)!=1) wrong();
    				continue;}
    			else if (i==argc-1) wrong();
    			width=parse_w(argv[i+1]);
    			i++; continue;
    		}
    		break;
    	}
    	if (!(fstRO=fopen(argv[i],"r"))) {
    		puts("File not openable");
    		return -1;}
    	printf("%s opened read-only\n",argv[i]);
    	if (sflag==1) puts("-s set");
    	printf("width set to %d\n",width);
    	fclose(fstRO);
    	return 0;
    }
    edit: except it doesn't parse the filename...oh well
    edit2: okay now it does

    what is int retv in the top function?

    If I try to use this as a guide, i'll be pretty lost.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    parse_w makes sure the value passed to it is an integer, and returns that integer if so. retv is the integer variable into which the string argument is parsed and which is returned to the caller.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    Why? I'm not a computer science prof yet, but it seems like something where you should be able to spot the angles after a few months. I wouldn't want my students to just sleep thru their final project -- I'd have such a big hat they'd love me forever
    With a big hat you would even be able to pull off a bunny which is in contrast to what a teacher ought to do; perhaps a pointer or a nudge in the right direction. I was curious if something similar had been covered in class. However I would go with something along the lines that tabstop suggested or look at the man page of getopt() to get an idea of how to go about this.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    With a big hat you would even be able to pull off a bunny which is in contrast to what a teacher ought to do; perhaps a pointer or a nudge in the right direction. I was curious if something similar had been covered in class. However I would go with something along the lines that tabstop suggested or look at the man page of getopt() to get an idea of how to go about this.
    Hmm...there is a spectrum in that attitude. At one end, the prof could issue a handout and tell everyone that this is their completed assignment they are about to turn in, and explain in detail how they managed to do it. At the other, s/he could just demand an undescribed assignment, then wait until after the assignments had been handed in to explain what was suppose to be done, which I would agree that's not so helpful and could lead someone in just about any direction with very little clue about what the right one might be.

    tabstop's suggestion about using switch is definately the thing here. From looking at the assignment, I would say the most complicated part is actually dealing with the options. Since you should be able to optimize beyond what getopts will give you, doing that instead might make a big difference to the whole.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Thumbs down Don't be a slacker

    All I have to say is :

    http://www.conestogac.on.ca/registra...dishonesty.jsp

    Do your assignments yourself. First year is NOTHING compared to the stuff to come. If you're having THAT much trouble I would consider switching to another program because it doesn't get any easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which utility in UNIX
    By Ron in forum C Programming
    Replies: 15
    Last Post: 06-19-2008, 08:23 AM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  4. UNIX script
    By boshke in forum Linux Programming
    Replies: 2
    Last Post: 12-10-2001, 12:55 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM