Note that there are some explanatory texts on larger screens.

plurals
  1. POJunk Characters Outputted After Parsing From STDIN in C
    text
    copied!<p>I am getting a junk character to be output at the very end of some text that I read in:</p> <pre><code>hum 1345342342 ~Users/Documents ecabd459 //line that was read in from stdin event action: hum_? event timestamp: 1345342342 event path: ~Users/Documents event hash: ecabd459 </code></pre> <p>At the end of the event action value there is a '_?' garbage character that is output as well. That can be rectified by setting the variable's last position to the null terminator (<code>event.action[3] = '\0'</code>) which is all well and good, but I am perplexed by the fact that the other char array <code>event.hash</code> does not exhibit this type of behavior. I am creating/printing them in an identical manner, yet hash does not behave the same. </p> <p><strong>Note:</strong> I was considering maybe this was due to the hash value being followed strictly by a newline character(which I get rid of by the way), so I tested my program with re-ordered input to no avail (that is, added an additional space and word after the hash value's position on the line).</p> <p>The relevant code is below:</p> <pre><code>struct Event{ char action[4]; long timestamp; char* path; char hash[9]; }; // parse line and return an Event struct struct Event parseLineIntoEvent(char* line) { struct Event event; char* lineSegment; int i = 0; lineSegment = strtok(line, " "); while (lineSegment != NULL) { if (i &gt; 3) { printf("WARNING: input format error!\n"); break; } if (i == 0) strncpy(event.action, lineSegment, sizeof(event.action)-1); else if(i == 1) event.timestamp = atoi(lineSegment); else if(i == 2) { event.path = malloc(sizeof(lineSegment)); strcpy(event.path, lineSegment); } else if(i == 3) strncpy(event.hash, lineSegment, sizeof(event.hash)-1); lineSegment = strtok(NULL, " "); i++; } // while return event; } // parseLineIntoEvent() int main (int argc, const char * argv[]) { //... printf("%s\n",line); //prints original line that was read in from stdin struct Event event = parseLineIntoEvent(line); printf("event action: %s\n", event.action); printf("event timestamp: %lu\n", event.timestamp); printf("event path: %s\n", event.path); printf("event hash: %s\n", event.hash); free(event.path); free(line); //... return 0; } </code></pre> <p><strong>EDIT:</strong> I read in a line with this function, which gets rid of the newline character:</p> <pre><code>// read in line from stdin, eliminating newline character if present char* getLineFromStdin() { char *text; int textSize = 50*sizeof(char); text = malloc(textSize); if ( fgets(text, textSize, stdin) != NULL ) { char *newline = strchr(text, '\n'); // search for newline character if ( newline != NULL ) { *newline = '\0'; // overwrite trailing newline } } return text; } </code></pre> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload