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 7933293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:17:15+00:00 2026-06-03T21:17:15+00:00

I have a simple file formatted as such NAME|VALUE NAME|VALUE NAME|VALUE I am trying

  • 0

I have a simple file formatted as such

NAME|VALUE
NAME|VALUE
NAME|VALUE

I am trying to read these in, and store them in an array of structs, the struct is this

struct data
{
    char* name;
    char* value;
};

For now, I know the size of the array will be 3, so I did this:

struct data pairs[3];

Here is my code as I am trying to read it in from the file:

char *tempVal;
int i =0;
    if(file != NULL)
        {
            char curLine [128];
            while(fgets(curLine, sizeof curLine, stockFile) != NULL)
            {   

                tempVal = strtok(curLine,"|");
                printf("i:%i\n",i);
                pairs[i].name= tempVal;
                printf("name at pos %i is %s\n",i, pairs[i].name);
                tempVal = strtok(NULL,"|");
                pairs[i].value= tempVal;
                printf("value at pos %i is %s\n",i, pairs[i].value);
                ++i;
            }
            fclose(file);
        }

and each of those printf statments prints the correct thing along the way, then I try to print the array with this

int j
for(j = 0; j < 3; j++)
    {   

        printf("ENTRY# %i\NAME:%s\VALUE:%s\n\n",j,pairs[j].name, pairs[j].value);
    }

Sorry the indentation is a little weird, tried messing with the code blocks but couldn’t get it quite perfect. However, I am wondering why it shows the correct thing during the while loop as it goes along, but then after it is complete the for loop shows all three of the array entries having the same name(the value is correct for the third entry but for the first and second entries the value field contains half of the correct value for the third entry)

Thanks!

  • 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-03T21:17:16+00:00Added an answer on June 3, 2026 at 9:17 pm

    The value returned from strtok() will be pointing to an element in curLine, so all entries in the array of structs will be pointing to an element in curLine which is overwritten with each call to fgets() (and will only be valid for the current iteration).

    You should make a copy of the value returned from strtok() during the while, possibly using strdup():

    while(fgets(curLine, sizeof curLine, stockFile) != NULL)
    {   
        tempVal = strtok(curLine,"|");
        printf("i:%i\n",i);
        pairs[i].name = strdup(tempVal);
        printf("name at pos %i is %s\n",i, pairs[i].name);
        tempVal = strtok(NULL,"|");
        pairs[i].value = strdup(tempVal);
        printf("value at pos %i is %s\n",i, pairs[i].value);
        ++i;
    }
    

    free() them later when no longer required:

    for (j = 0; j < 3; j++)
    {
        free(pairs[j].name);
        free(pairs[j].value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a formatted 2D array from a file on disk
i have a simple xml file in a wcf service that i am trying
In my application I have a simple XML formatted file containing structured data. Each
I have a simple file browser and there I display files and folders, obtained
I have a simple file-system-ed-login (not mysql) just to track number of users who
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a simple cpp file which uses GIO. I have stripped out everything
I have a simple xml file and I want to remove everything before the
I have this simple XML file : <?xml version=1.0 encoding=utf-8 ?> <Artists> <artist artistId=1>
In my app, I have a simple ASCII file which stores some config info

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.