Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:51:04+00:00 2026-05-27T23:51:04+00:00

I am getting a junk character to be output at the very end of

  • 0

I am getting a junk character to be output at the very end of some text that I read in:

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

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 (event.action[3] = '\0') which is all well and good, but I am perplexed by the fact that the other char array event.hash does not exhibit this type of behavior. I am creating/printing them in an identical manner, yet hash does not behave the same.

Note: 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).

The relevant code is below:

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 > 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;
}

EDIT:
I read in a line with this function, which gets rid of the newline character:

// 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;
}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:51:04+00:00Added an answer on May 27, 2026 at 11:51 pm

    but I am perplexed by the fact that the other char array event.hash does not exhibit this type of behavior

    You got unlucky. hash[8] may have gotten a ‘\0’ by sheer (bad-)luck.

    Try setting it to something “random” before your strtok loop

        int i = 0;
        event.hash[8] = '_';             /* forcing good-luck */
        lineSegment = strtok(line, " ");
        while (lineSegment != NULL) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Nodelist.item[index].getChildNodes(), i am getting some junk values in between all the nodes
I'm getting a junk of JSON data and would like to work with that
I am getting some junk data returned from a ToString() call on a DateTime
I want to eliminate the junk that I'm getting on the vector<int> synsAux below.
Getting 401 errors when trying to use ASP.NET back end in load balanced environment
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
I am trying to parse XML with Python but not getting very far. I
I've got some weird images I'm getting from some hardware at work. They're from
Since I am very UNHAPPY with Visual Studio (It's just Junk!) I am trying
I have seen in a google search that some other people have had this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.