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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:03:11+00:00 2026-06-13T00:03:11+00:00

I have a function that reads an input file and is supposed to modify

  • 0

I have a function that reads an input file and is supposed to modify the contents of a char** and a int*. The function is as follows:

void
input_parser(arguments* args, char** input, int* files) {
   char buffer[MAX];
   FILE *fr;
   fr = fopen(args->file,"r");
   if (fr == NULL) {
       printf("No correct input file was entered\n");
       exit(0);
   } 
   while(fgets(buffer,MAX,fr) != NULL) {
       input[*files] = strtok(buffer,"\n");
       (*files)++;
   }
   fclose(fr);
   return;
}

I have defined input and files as follows in the main program:

char* input[25];
files = 0;

I call the function as follows:

input_parser(args, input, &files);

The input file contains 3 lines as follows:

output1.xml
output2.xml
output3.xml

I notice that during the while loop the ‘current’ value is read correctly but stored in all input[*] resulting in:

input[0] = output3.xml
input[1] = output3.xml
input[2] = output3.xml

I would greatly appreciate if someone has any idea what is going wrong here.

  • 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-13T00:03:13+00:00Added an answer on June 13, 2026 at 12:03 am

    The function is storing the address of the local variable buffer to each element in the input array: you need to copy the value returned by strtok(). The code as it stands is undefined behaviour as the buffer is out of scope once input_parser() returns, even it was not the logic is incorrect anyway.

    If you have strdup(), you just use it:

    input[*files] = strdup(strtok(buffer,"\n")); /* NULL check omitted. */
    

    otherwise malloc() and strcpy(). Remember to free() the elements of input when no longer required.

    Initialise input to be able determine which elements point to valid strings:

    char* input[25] = { NULL };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that reads integers with certain format from a file. It
I have an example function below that reads in a date as a string
I have a function, that reads a registry key, get a path for a
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have a function that searches for a string inside a text file. I
I have a function that converts a .csv file to a datatable. One of
I have a file that I'm working with. I want to create a function
I have a php file that reads data from my mysqldb as a json
I've got a piece of an application that reads input sometimes from a file,
I want a function that reads in a file and returns the sum of

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.