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.