I think something in my printTable() is messing it up:
Code:void printTable (struct table * symbols, FILE * out) {
struct entry *n = symbols->first;
printf("%s\n", symbols->first->symbol);
printf("%s\n", n->symbol);
//line above prints %s and then a newline
while (n != NULL) {
printf("%s", n->symbol);
//this line prints a newline even though I didn't give it anything
printf("after");
printf("%d\t", n->linedef);
struct lineNode *nline = n->occur;
while ((nline != NULL) && (nline->lineoccur != -1)) {
printf("%d\t", nline->lineoccur);
nline = nline->next;
}
printf("\n");
fprintf(out, "%s\t", n->symbol);
fprintf(out, "%d\t\n", n->linedef);
n = n->next;
}
printf("finished printing \n");
}
In tabletest.c I am using a nextWord function to get the next word and add to table:
my main is just looping and reading one line in the input file, "abc"Code:int nextWord (char *str, int pos, char *word, int linecount, struct table *p) {
printf("string is %s\n", str);
if (emptyLine(str)== -1) {
printf("empty line\n");
return -1;
}
int i=0;
while (1) {
if (*str == '\n' || *str == '\0' || *str == '#')
return -1;
else if (i == pos) {
break;
}
else {
i++;
str++;
}
}
printf("str is now %s\n", str);
int r = pos;
char* word2 = word;
int j;
for (j=0; *str!='\n' || *str!=EOF; str++) {
if (*str != ' ') {
char c = *str;
while (c != '\n' && c != ' ' && c!='\0') {
r++;
*word = c;
word++;
str++;
c = *str;
}
*word = '\0';
printf("word is %s\n", word2);
char last = *--word;
//fprintf(output, "%c \n", last);
if (last != ':') {
printf("adding use\n");
addUse(word2, linecount, p);
printTable(p, output);
return r;
}
else {
printf("adding def\n");
addDef(word2, linecount, p);
printTable(p, output);
return r;
}
printf("here %s %d\n", word2, linecount);
break;
}
else {
r++;
j++;
}
}
word = word2;
return r;
}
Is there any reason why printTable would print out a newline and mess up the formatting?Code:main() {
printf("stack trace 1\n");
input = fopen("table.test", "r");
output = fopen("table.out", "w");
char *s;
int linecount = 1;
int pos;
char line[MAXLINE];
char word[MAXSYMBOL];
struct table *p = newTable();
printf("stack trace 1\n");
//processLine(s, 1, p);
//printTable(p, output);
/*while (s = newLine(input)) {
printf("reading input: %s\n", s);
processLine(s, linecount);
linecount++;
}*/
while (s = newLine(input)) {
printf("reading input: %s\n", s);
pos = 0;
while (1) {
printf("should go in here\n");
if ((pos = nextWord (s, pos, word, linecount, p)) == -1)
break;
printf("pos is %d\n", pos);
printTable(p, output);
}
linecount++;
}
stack trace 1
stack trace 1
reading input: abc
should go in here
string is abc
line[i] is a
should not be empty
str is now abc
word is abc
adding use
starting to add abc
lineNum is 1
going here
added word abc
finished adding use
abc <--ok
abc <--ok
abc <--newline??
after-1 1
finished printing
pos is 4
abc
abc
abc
after-1 1
finished printing
should go in here
string is abc
line[i] is a
should not be empty
reading input:
should go in here
string is
line[i] is