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

  • SEARCH
  • Home
  • 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 8948365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:58:03+00:00 2026-06-15T12:58:03+00:00

I have some C code to parse a text file, first line by line

  • 0

I have some C code to parse a text file, first line by line and then into tokens

This is the function that parses it line by line:

int parseFile(char *filename) {
//Open file
FILE *file = fopen(filename, "r");
//Line, max is 200 chars
int pos = 0;
while (!feof(file)) {
    char *line = (char*) malloc(200*sizeof(char));
    //Get line
    line = fgets(line, 200, file);
    line = removeNewLine(line);
    //Parse line into instruction
    Instruction *instr = malloc(sizeof(instr));
    instr = parseInstruction(line, instr);
    //Print for clarification
    printf("%i: Instr is %s arg1 is %s arg2 is %s\n", 
        pos,
        instr->instr,
        instr->arg1,
        instr->arg2);
    //Add to end of instruction list
    addInstruction(instr, pos);
    pos++;
    //Free line
    free(line);
}
return 0;

}

And this is the function that parses each line into some tokens and eventually puts it into an Instruction struct:

Instruction *parseInstruction(char line[], Instruction *instr) {
//Parse instruction and 2 arguments
char *tok = (char*) malloc(sizeof(tok));
tok = strtok(line, " ");
printf("Line at %i tok at %i\n", (int) line, (int) tok);
instr->instr = tok;
tok = strtok(NULL, " ");
if (tok) {
    instr->arg1 = tok;
    tok = strtok(NULL, " ");
    if(tok) {
        instr->arg2 = tok;
    }
}
return instr;

}

the line printf("Line at %i tok at %i\n", (int) line, (int) tok); in ParseInstruction always prints the same two values, why are these pointer addresses never changing? I have confirmed that parseInstruction returns a unique pointer value each time, but each instruction has the same pointer in it’s instr slot.

Just for clarity, Instruction is defined like this:

typedef struct Instruction {

char *instr;
char *arg1;
char *arg2;

} Instruction;

What am I doing wrong?

  • 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-06-15T12:58:05+00:00Added an answer on June 15, 2026 at 12:58 pm

    That is how strtok works: it actually modifies the string that it’s operating on, replacing separator-characters with '\0' and returning pointers into that string. (See the “BUGS” section in the strtok(3) manual page, though it’s not really a bug, just a behavior-that-people-don’t-usually-expect.) So your initial tok will always point to the first character of line.

    By the way, this:

    char *tok = (char*) malloc(sizeof(tok));
    tok = strtok(line, " ");
    

    first sets tok to point at the return-value of malloc, then re-assigns it to point at the return-value of strtok, thereby completely discarding the return-value of malloc. It’s just like how writing this:

    int i = some_function();
    i = some_other_function();
    

    completely discards the return-value of some_function(); except that it’s even worse, because discarding the return-value of malloc results in a memory leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some C code that parses a file and generates another file of
I have some data in a text file that looks like this: 1895723957 8599325893
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I have some parsing code that allows for escape sequences to be entered into
I have a simple text file that is ~150mb. My code will read each
I have a Ruby code that reads file line-by-line and checks if it needs
I have some code that looks more or less like this: while(scanner.hasNext()) { if(scanner.findInLine(Test)
I use this bit of code to feed some data i have parsed from
I have some code that will change the background color of a specific label
I have some code here that uses bitsets to store many 1 bit values

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.