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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:20:06+00:00 2026-06-15T17:20:06+00:00

I have a trouble reading a .txt file into a char* array. I have

  • 0

I have a trouble reading a .txt file into a char* array.

I have a levels.txt file that looks like this:

level1.txt
level2.txt

I have my array defined inside a class as

 char* levels[10];

And my parsing function looks like this:

// Parse the level list file
int Environment::parseLevels() {
    ifstream data;
    data.open("levels.txt");

    char buf[64];

    for (int i=0; i<sizeof(levels); i++) {
        data.getline(buf, 64);
        levels[0] = strtok(buf, " ");
    }
}

If I do cout << levels[0]; right after

levels[0] = strtok(buf, " ");

then I get a fine output. However, when I try to cout << levels[0]; from somewhere else, nothing gets displayed.

What am I doing wrong?

Thank you 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-06-15T17:20:07+00:00Added an answer on June 15, 2026 at 5:20 pm

    The pointer returned by strtok is not going to point to valid memory forever since the buffer that you’re tokenizing is declared on the stack. You’ll need to actually copy the string being pointed to by the return pointer of strtok, not the pointer itself if you want to use the string outside the function body.

    So basically modify your code to the following:

    levels[0] = new char[64];
    char* temp = strtok(buf, " ");
    
    //check for NULL pointer return from strtok()
    if (temp) 
    {
        //if the pointer is not NULL, copy the contents of the temporary string
        //returned by strtok into more permanent memory allocated on the heap
        //and being pointed to by levels[0]
    
        //Use strncpy() to prevent the risk of a buffer overflow
        strncpy(levels[0], temp, 64);
    }
    

    Then in the destructor for your Environment object, make sure to have some loop to free the memory being pointed to by each of the members of the levels array that are pointing to memory allocated via new []. You do this by calling delete []

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

Sidebar

Related Questions

I have some trouble reading file in Java. What a file looks like: Answer
I have trouble figuring out why a simple division like this one always returns
I have trouble finding way to correctly refactor this code so that there would
I just started C# today, and i have trouble reading an excel file. Here's
I am having trouble reading a text file to a list, what I have
I have a project that is like an IPOD playlist . Im having trouble
I have some trouble reading from an ini file using boost program options. The
I'm having some trouble reading from an Excel spreadsheet in C#. I have this
I am having trouble reading data from a file into a list in a
I'm having trouble reading in supplementary unicode characters using Java. I have a file

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.