Thread: Simple Code, looking for input.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    Simple Code, looking for input.

    hi everyone,

    i wrote this little piece of code, because mostly i got sick of my TA not being able to read my code.

    i code using VI w/ a tabspace of 4, he opens it and the editor defaults to 8, w/h makes my code look like something out of the obfuscation contest.

    anyhow...simple piece of code....takes 3 cmd line args. input file, output file and size to tab.

    it replaces each '\t' character w/ a number of spaces.

    any idea on improventes or comments are welcome.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Be careful with
    Code:
    char c;
    while((c = fgetc(in))) 
    {
            if (EOF == c)
            {
                break;
            }
    }
    First of all you don't need comments here. Everyone can see your checking for EOF. Most people code this as
    Code:
    int c;
    while((c = fgetc(in)) != EOF) 
    {
    }
    As the break statement looks ugly there. It should be
    int c as char's arn't guranteed to be signed. If char's are
    unsigned and as EOF is negative = -1 on linux this can cause problems.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >on linux this can cause problems
    On any system this will cause problems for the reason you stated.

    -Prelude
    Last edited by Prelude; 03-03-2002 at 07:34 AM.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    Hi!

    Itīs a style question:
    just turn
    Code:
     if ('\t' == c)
            {
    			for (count = 0; count <	tab; count++)
                	fputs(" ",out);
            }
    into
    Code:
    if (c == '\t')
       for(count = 0; count < tab; count++) fputs(' ',out);
    and
    Code:
    if (EOF == c)	
            {
                break;
            }
    into
    Code:
    if (c == EOF)
       break;
    Itīs more common, more readable and prevents misstakes.

    I also wonder if
    fputs(" ",out);
    works like expected. I would rewrite it as
    fputs(' ',out);

    klausi
    Last edited by klausi; 03-03-2002 at 11:42 AM.
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM