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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:40:17+00:00 2026-05-30T19:40:17+00:00

This is my code: #define LEN 40 #define STUDLIST ./students.txt int main() { FILE

  • 0

This is my code:

#define LEN 40
#define STUDLIST "./students.txt"

int main()
{
 FILE * studd;
 char del[] = "" " '\n'";
 char name[LEN], surname[LEN], str[LEN];
 char *ret;
 char *tokens[2] = {NULL};
 char *pToken = str;
 unsigned int i = 0;

 /* open file */
 if ( (studd = fopen(STUDLIST,"r") ) == NULL ) 
 {
   fprintf(stderr, "fopen\n");
   exit(EXIT_FAILURE);
 }

 while((ret = fgets(str, LEN, studd)))
    {
     if(ret)
        {
         for( tokens[i] = strtok_r( str, del, &pToken ); ++i < 2; 
              tokens[i] = strtok_r( NULL, del, &pToken ) );

           strcpy(name, tokens[0]);
           strcpy(surname, tokens[1]);

           printf( "name = %s\n", name );
           printf( "surname = %s\n", surname );
         }
      fflush(studd);
    }
   fclose(studd);

 return 0;
}

Here there is the file students.txt: http://pastebin.com/wNpmXYis

I don’t understand why the output isn’t correct as I expected.
I use a loop to read each line with fgets, then I have a sting composed by [Name Surname], and I want to divide it in two different strings ([name] and [surname]) using strtok_r. I tried with a static string and it works well, but If I read many strings from FILE the output is not correct as you can see here:

http://pastebin.com/70uPMzPh

Where is my fault?

  • 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-30T19:40:18+00:00Added an answer on May 30, 2026 at 7:40 pm

    You start i at zero:

    unsigned int i = 0;
    

    And later you increment it:

    ++i < 2; 
    

    You never set i back to zero, and in fact, continue incrementing i again for every new line in your file. With 14 names in your input file, I expect i to get to about 14.
    (or maybe 13 or 15, depending on the exact logic).

    So this line:

    tokens[i] = strtok_r(...);
    

    ends up putting strtok results into tokens[2..15]. But only tokens[0] and tokens[1] are valid. Everything else is undefined behavior.

    Answer: Be sure you reset i to zero when you read a new line of your file.

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

Sidebar

Related Questions

We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
#include <stdio.h> #include <ctype.h> #define STRING_LEN 500 void stripspaces(char, char, char); int main(void) {
I have a function like this #define LEN 10 #define MAX 200 void f(char
I have this code #define BUFFER_LEN (2048) static float buffer[BUFFER_LEN]; int readcount; while ((readcount
I occasionally write code something like this: // file1.cpp #define DO_THIS 1 #if DO_THIS
I have some Objective-C code that looks like this: #define myVar 10 float f
I'm trying to define a function for the NodeList object. This is the code:
This code receives a input file with 10 filenames, stores them into an 2d
When I was reading the nginx code, I have seen this function : #define
Point of HW is it should read this from a file: int func(int arg)

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.