Output Displaying Incorrectly

This is a discussion on Output Displaying Incorrectly within the C Programming forums, part of the General Programming Boards category; Hello, I'm currently working an assignment that is a simplified version of the UNIX join command. Here is my code: ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Output Displaying Incorrectly

    Hello,

    I'm currently working an assignment that is a simplified version of the UNIX join command. Here is my code:

    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
     
     #define MAX_FIELDS 100
     
     main(int argc, char *argv[]) {
        char buf[BUFSIZ]; // read line from file 1 here
        char buf2[BUFSIZ]; // read line from file 2 here
        char line1[MAX_FIELDS][BUFSIZ]; // array for fields from f1
        char line2[MAX_FIELDS][BUFSIZ]; // array for fields from f2
        char *tok;
        FILE *fp1;  // file 1
        FILE *fp2;  // file 2
        int pos1;  // field position in f1
        int pos2;  // field position in f2    
        int posit1;  // spot for inserting into line1
        int posit2;  // spot for inserting into line2
        int i;
        int j;
    
        
        // set field positions for comparisons
        if (argc == 3) {
           pos1 = 0;
           pos2 = 0;
        }
        else if (argc > 3) {
           if (strcmp(argv[1],"-1") == 0) pos1 = atoi(argv[2])-1;
           else if (strcmp(argv[1],"-2") == 0) {
              pos1 = 0;
              pos2 == atoi(argv[2])-1;
           }
           else if (strcmp(argv[3],"-2") == 0) pos2 = atoi(argv[4])-1;
        }
        
        // open file 1
        if (strcmp(argv[argc-2],"-") == 0) fp1 = stdin;
        else if ( (fp1 = fopen(argv[argc-2],"r")) == NULL) {
           fprintf(stderr,"Cannot open %s\n",argv[argc-2]);
           exit(1);
        }
        
        // open file 2
        if (strcmp(argv[argc-1],"-") == 0) fp2 = stdin;
        else if ( (fp2 = fopen(argv[argc-1],"r")) == NULL) {
           fprintf(stderr,"Cannot open %s\n",argv[argc-1]);
           exit(2);
        }
        
        while ( fgets(buf,BUFSIZ,fp1) != NULL ) {
           posit1 = 0;
           for (tok = strtok(buf," "); tok != NULL; tok = strtok(NULL," ")) {
              strcpy(line1[posit1],tok);
              posit1++;
           }
           
           while ( fgets(buf2,BUFSIZ,fp2) != NULL ) {
              posit2 = 0;
              for (tok = strtok(buf2," "); tok != NULL; tok = strtok(NULL," ")) {
                 strcpy(line2[posit2],tok);
                 posit2++;
              }
              
              if (strcmp(line1[pos1],line2[pos2]) == 0) {
                 printf("%s ", line1[pos1]);
                 for (i = 0; i < posit1; i++)
                    if (i != pos1) printf("%s ", line1[i]);
                 for (i = 0; i < posit2; i++)
                    if (i != pos2) printf("%s ", line2[i]);
              }
           }
        }
     }
    This compiles and runs correctly. However, the ouput doesn't work right. The reason is, when I tokenize the line from the first file, I'm taking the newline character with the last token. So, when I do the print statement, it puts a newline at the end of the line1 tokens. Is there some way to output the line 1 array without the newline character at the end? Normally I would just tokenize the newline character off the end of the line, but I don't know how long the line will be.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    for (tok = strtok(buf," "); tok != NULL; tok = strtok(NULL," \n")) {
    You have the second argument specify more than one delimiter - this example delimits on both space and newline.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Oh wow, I didn't know you could do that. Thanks!

    Now, I'm having another problem that I didn't notice before. The problem is, it seems like only the first line from file 1 is being read in. When I give it files like this:

    File1 (names):
    B0001 Dot CS
    B0003 Cat MA
    B0006 Bev CH
    B0010 Ann CS

    File2 (grades):
    B0001 cs1101 A
    B0001 cs2132 B
    B0001 ma1010 C
    B0002 cs2132 A
    B0004 cs2132 C
    B0006 cs2132 D
    B0006 cs2112 A

    And then run it like this:
    myjoin names grades

    I get the following output:
    B0001 Dot CS cs1101 A
    B0001 Dot CS cs2132 B
    B0001 Dot CS ma1010 C

    It looks like it's only comparing the first line of file 1 to the other lines in file 2. I should get output like this:

    B0001 Dot CS cs1101 A
    B0001 Dot CS cs2132 B
    B0001 Dot CS ma1010 C
    B0006 Bev CH cs2132 D
    B0006 Bev CH cs2112 A

    Any ideas?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The first time through the outer while loop, the inner while reads to the end of the file -- and it remains at the end. After the inner while loop, you need to go back to the beginning of the file.
    Code:
    rewind(fp2);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. displaying a array to output
    By gkoenig in forum C Programming
    Replies: 19
    Last Post: 02-11-2008, 03:18 AM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 07:08 AM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 05:41 PM
  4. Displaying my glut output in my own class window.
    By Queatrix in forum Windows Programming
    Replies: 0
    Last Post: 10-19-2005, 10:09 AM
  5. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21