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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:48:40+00:00 2026-05-20T18:48:40+00:00

I have a question related to strings and pointers. Please explain only with C/C++

  • 0

I have a question related to strings and pointers.
Please explain only with C/C++ programs….

There is a file, which contains 1 word in each line.
I knew the no. of words that are there in the file. Please explain with the help of small code, how I can store those words in RAM, efficiently.

Is fscanf(fp,"%s",word) & strcpy , the only way to store the words in RAM… no other efficient algo or logic available..

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-05-20T18:48:41+00:00Added an answer on May 20, 2026 at 6:48 pm

    Probably the most efficient way to do this is to read the whole file into memory in one block (use fread). Then allocate an array of pointers, one for each word. Then walk through the file in memory, changing the \n characters to \0 and storing a pointer the the character after each \0 in your array.

    It is efficient because it only performs one I/O operation, two memory allocations, and loops over the characters in the file twice (once to copy them to the buffer, and once again to break them up into separate strings). The algorithm you describe (fscanf and strcpy) will perform many I/O operations, allocate memory for each word, and loops over the characters at least three times (once to read into the buffer, once to find the length to allocate memory for, and once to copy from the buffer into the allocated memory).

    Here’s a simple version with no error checking:

    char* buffer; // pointer to memory that will store the file
    char** words; // pointer to memory that will store the word pointers
    
    // pass in FILE, length of file, and number of words
    void readfile(FILE *file, int len, int wordcnt)
    {
        // allocate memory for the whole file
        buffer = (char*) malloc(sizeof(char) * len);
        // read in the file as a single block
        fread(buffer, 1, size, file);
    
        // allocate memory for the word list
        words = (char**) malloc(sizeof(char*) * wordcnt);
        int found = 1, // flag indicating if we found a word
                       // (starts at 1 because the file begins with a word)
            curword = 0; // index of current word in the word list
    
        // create a pointer to the beginning of the buffer
        // and advance it until we hit the end of the buffer
        for (char* ptr = buffer; ptr < buffer + len; ptr++)
        {
            // if ptr points to the beginning of a word, add it to our list
            if (found)
                words[curword++] = ptr;
            // see if the current char in the buffer is a newline
            found = *ptr == '\n';
            // if we just found a newline, convert it to a NUL
            if (found)
                *ptr = '\0';
        }
    }
    

    Here’s a slightly simpler version using strtok:

    char* buffer;
    char** words;
    
    void readfile(FILE *file, int len, int wordcnt)
    {
        buffer = (char*) malloc(sizeof(char) * len);
        fread(buffer, 1, size, file);
        buffer[len] = '\0';
    
        words = (char**) malloc(sizeof(char*) * wordcnt);
        int curword = 0;
        char* ptr = strtok(buffer, "\n");
        while (ptr != NULL)
        {
            words[curword++] = ptr;
            ptr = strtok(NULL, "\n");
        }
    }
    

    Note that the above two examples assume that the last word in the file ends with a newline!

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

Sidebar

Related Questions

I have a question related to matching strings in a MSSQL database. Basically, I
question related to this I have a string a\;b\\;c;d which in Java looks like
Related to my previous question , I have a string on the following format:
I have question related to what is done in SMS application is if i'm
I have a question related to how relative paths are interpreted in various environments
I have a question related to this one . I don't want to do
I have a question related to this one : I'm trying to attach an
I have a question related to understanding of how python dictionaries work. I remember
I have following event class. I have a question related to the Property method
I have a simple question related to one-line programming. First an example: function test(a)

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.