Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ugly C code

Ugly C code

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionperl
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meixiang6
    wrote on last edited by
    #1

    Following to my question yesterday, is there anything that can be done to clean up the c code below which works fine but looks ugly especially the strcat's? The program parses a file containing a list of files and then runs a command on each. I can do a similar thing in perl like this a couple of lines but in C its looks ugly and I think its because I am a newbie...any comments will be appreciated.... thanks in advance for everyones help In perl: #!/usr/bin/perl while(<>){ chomp; $cmd="qsub -P 9999 -wd -e $_\.error -b y /usr/local/projects/bin/program -i $_ -type p -terms -lookup -o $_\.qsub"; print "$cmd\n"; } in C: #include #include #include #include #include #include int main(int argc, char *argv[]) { char * cwd = getcwd(0,0); char pro[10000]; char str[1000]; char* nl; if (argc < 3) { printf("\n\nPlease supply a file containing a list of single file names follow by project code\n"); exit(5); } FILE *f; f = fopen(argv[1], "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { nl = strrchr(str, '\r'); if (nl) *nl = '\0'; nl = strrchr(str, '\n'); if (nl) *nl = '\0'; strcpy(pro, "qsub -P "); strcat(pro, argv[2]); strcat(pro, " -wd -e "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, ".error -b y /usr/local/projects/bin/program -i "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, " -type p -terms -lookup -o "); strcat(pro, str); strcat(pro, ".qsub"); printf (pro); system(pro); } }

    L G 2 Replies Last reply
    0
    • M meixiang6

      Following to my question yesterday, is there anything that can be done to clean up the c code below which works fine but looks ugly especially the strcat's? The program parses a file containing a list of files and then runs a command on each. I can do a similar thing in perl like this a couple of lines but in C its looks ugly and I think its because I am a newbie...any comments will be appreciated.... thanks in advance for everyones help In perl: #!/usr/bin/perl while(<>){ chomp; $cmd="qsub -P 9999 -wd -e $_\.error -b y /usr/local/projects/bin/program -i $_ -type p -terms -lookup -o $_\.qsub"; print "$cmd\n"; } in C: #include #include #include #include #include #include int main(int argc, char *argv[]) { char * cwd = getcwd(0,0); char pro[10000]; char str[1000]; char* nl; if (argc < 3) { printf("\n\nPlease supply a file containing a list of single file names follow by project code\n"); exit(5); } FILE *f; f = fopen(argv[1], "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { nl = strrchr(str, '\r'); if (nl) *nl = '\0'; nl = strrchr(str, '\n'); if (nl) *nl = '\0'; strcpy(pro, "qsub -P "); strcat(pro, argv[2]); strcat(pro, " -wd -e "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, ".error -b y /usr/local/projects/bin/program -i "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, " -type p -terms -lookup -o "); strcat(pro, str); strcat(pro, ".qsub"); printf (pro); system(pro); } }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, why don't you use sprintf() to compute the entire string? it works similar to printf() but outputs to a char buffer. FYI: More modern languages support string objects and do simple concatenation, much like ancient Basic: string c = a + b; :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • M meixiang6

        Following to my question yesterday, is there anything that can be done to clean up the c code below which works fine but looks ugly especially the strcat's? The program parses a file containing a list of files and then runs a command on each. I can do a similar thing in perl like this a couple of lines but in C its looks ugly and I think its because I am a newbie...any comments will be appreciated.... thanks in advance for everyones help In perl: #!/usr/bin/perl while(<>){ chomp; $cmd="qsub -P 9999 -wd -e $_\.error -b y /usr/local/projects/bin/program -i $_ -type p -terms -lookup -o $_\.qsub"; print "$cmd\n"; } in C: #include #include #include #include #include #include int main(int argc, char *argv[]) { char * cwd = getcwd(0,0); char pro[10000]; char str[1000]; char* nl; if (argc < 3) { printf("\n\nPlease supply a file containing a list of single file names follow by project code\n"); exit(5); } FILE *f; f = fopen(argv[1], "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { nl = strrchr(str, '\r'); if (nl) *nl = '\0'; nl = strrchr(str, '\n'); if (nl) *nl = '\0'; strcpy(pro, "qsub -P "); strcat(pro, argv[2]); strcat(pro, " -wd -e "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, ".error -b y /usr/local/projects/bin/program -i "); strcat(pro, cwd); strcat(pro, "/"); strcat(pro, str); strcat(pro, " -type p -terms -lookup -o "); strcat(pro, str); strcat(pro, ".qsub"); printf (pro); system(pro); } }

        G Offline
        G Offline
        grzkas
        wrote on last edited by
        #3

        You should really use sprintf() instead of strcpy and strcat, it will look something like this: sprintf(pro, "%s%s%s%s%c%s%s%s%c%s%s%s%s", "qsub -P ", argv[2], " -wd -e ", cwd, '/', str, ".error -b y /usr/local/projects/bin/program -i ", cwd, '/', str, " -type p -terms -lookup -o ", str, ".qsub"); after this call pro[] buffer contains formatted string :-) And few remarks about your code safety: be careful with creating huge local buffers on the stuck - pro[] buffer in this case. It may happen that you get stuck overflow (application crush) It is better to create such buffers dynamically then you can check if allocation succeeded or just create static buffer use sizeof(str) in fgets(str, sizeof(str), f) to avoid problems with writing outsie str[] memory area when str[] buffer size will be changed to smaller one

        M 1 Reply Last reply
        0
        • G grzkas

          You should really use sprintf() instead of strcpy and strcat, it will look something like this: sprintf(pro, "%s%s%s%s%c%s%s%s%c%s%s%s%s", "qsub -P ", argv[2], " -wd -e ", cwd, '/', str, ".error -b y /usr/local/projects/bin/program -i ", cwd, '/', str, " -type p -terms -lookup -o ", str, ".qsub"); after this call pro[] buffer contains formatted string :-) And few remarks about your code safety: be careful with creating huge local buffers on the stuck - pro[] buffer in this case. It may happen that you get stuck overflow (application crush) It is better to create such buffers dynamically then you can check if allocation succeeded or just create static buffer use sizeof(str) in fgets(str, sizeof(str), f) to avoid problems with writing outsie str[] memory area when str[] buffer size will be changed to smaller one

          M Offline
          M Offline
          meixiang6
          wrote on last edited by
          #4

          Thank you so much grzkas!:thumbsup:

          S 1 Reply Last reply
          0
          • M meixiang6

            Thank you so much grzkas!:thumbsup:

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            One little (possible) improvement to the original answer - the literal strings can be embedded in the sprintf format string, as shown below. It's a bit more like variable interpolation in strings, a la Perl.

            sprintf(pro, "qsub -P %s -wd -e %s/%s.error -b y /usr/local/projects/bin/program -i %s/%s -type p -terms -lookup -o %s.qsub",
            argv[2],
            cwd,
            str,
            cwd,
            str,
            str);

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M 1 Reply Last reply
            0
            • S Stuart Dootson

              One little (possible) improvement to the original answer - the literal strings can be embedded in the sprintf format string, as shown below. It's a bit more like variable interpolation in strings, a la Perl.

              sprintf(pro, "qsub -P %s -wd -e %s/%s.error -b y /usr/local/projects/bin/program -i %s/%s -type p -terms -lookup -o %s.qsub",
              argv[2],
              cwd,
              str,
              cwd,
              str,
              str);

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              M Offline
              M Offline
              meixiang6
              wrote on last edited by
              #6

              Thanks Stuart, very useful..

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups