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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:47:33+00:00 2026-05-27T19:47:33+00:00

Im trying to work with the example in the K and R book for

  • 0

Im trying to work with the example in the K and R book for this topic, but struggling.

I want an array of Char Arrays, whereby each element of the ‘Father’ Array points to an array of characters (string). Basically, I am reading from a file, line at a time, storing each line into an array, and then trying to store that array, into another array, which I can then sort via qsort.

But I can’t seem to get anywhere with this! Anyhelp on my code is much appreciated, i.e. where to go from where I am!

EDIT: The problem is, the printing function isn’t printing out my words that should be within the array of arrays, instead its just printing garbage, the main problem is, I’m not sure whether i am de-referencing things correctly, or not at all, whether I am adding it to the array of arrays correctly etc.

Regards.

#define MAXLINES 5000 /* max no. lines to be stored */
#define MAXLEN 1000 /* max length of single line */

char *lineptr[MAXLINES];

void writelines(char *lineptr[], int nlines);

int main(int argc, char *argv[]) {
 int nlines = 0, i, j, k;
 char line[MAXLEN];
 FILE *fpIn;
 fpIn = fopen(argv[1], "rb");
 while((fgets(line, 65, fpIn)) != NULL) {
     j = strlen(line);
     if (j > 0 && (line[j-1] == '\n')) {
         line[j-1] = '\0';
     }
     if (j > 8) {
         lineptr[nlines++] = line;
     }
 }  
 for(i = 0; i < nlines; i++)
     printf("%s\n", lineptr[i] );
return 0;    
}
  • 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-27T19:47:34+00:00Added an answer on May 27, 2026 at 7:47 pm

    Dan definitely found one error, the identical storage. But I think there are more bugs here:

    while((fgets(line, 65, fpIn)) != NULL) {
    

    Why only 65? You’ve got MAXLEN space to work with, you might as well let your input be a bit longer.

        j = strlen(line);
        if (j > 0 && (line[j-1] == '\n')) {
            line[j-1] = '\0';
        }
        if (j > 8) {
            lineptr[nlines++] = line;
        }
    }
    

    Why exactly j > 8? Are you supposed to be throwing away short lines? Don’t forget to deallocate the memory for the line in this case, once you’ve moved to the dynamic allocation that Dan suggests.

    Update

    ott recommends strdup(3) — this would be easy to fit into your existing system:

    while((fgets(line, 65, fpIn)) != NULL) {
        j = strlen(line);
        if (j > 0 && (line[j-1] == '\n')) {
            line[j-1] = '\0';
        }
        if (j > 8) {
            lineptr[nlines++] = strdup(line);
        }
    }
    

    Dan recommended calloc(3), that would be only slightly more work:

    line = calloc(MAXLINE, sizeof char);
    while((fgets(line, 65, fpIn)) != NULL) {
        j = strlen(line);
        if (j > 0 && (line[j-1] == '\n')) {
            line[j-1] = '\0';
        }
        if (j > 8) {
            lineptr[nlines++] = line;
        line = calloc(MAXLINE, sizeof char);
        }
    }
    

    Of course, both these approaches will blow up if the memory allocation fails —
    checking error returns from memory allocation is always a good idea. And
    there’s something distinctly unbeautiful about the second mechanism.

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

Sidebar

Related Questions

I'm trying something like this, but this example does not work. jsObj = {};
I'm trying to understand why this does not work. (Basic example with no validation
I'm trying to get the FinchVideo example from the Programming Android book to work.
I,m trying to work through and test a Voice Recognition example based on the
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
I've seen some examples, but trying to better understand how Resource files work, such
I am trying to work out an example from ".NET Domain Driven Design with
I'm trying to work with sockets and I have such problem In code example:
I'm trying to follow the example in this post by tvanfosson . I just
I'm trying to use the example for Jquery-ui autocomplete to work with Freebase. Additionally

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.