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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:04:36+00:00 2026-05-26T10:04:36+00:00

I was so pleased with myself getting this to work last night without any

  • 0

I was so pleased with myself getting this to work last night without any errors or warnings on my first try too! But, of course, I changed a bunch of stuffed and screwed it up…
When I tried to gdb it, the list from this.ytxt seemed load to memory just fine. I think the problem was writing it. Now. it works again but only writes the first line of the file. I commented out whole functions and printf’d test marks and still couldn’t figure it out.
the idea is to read a variable number of lines from a file and print them in pairs. (Actually
it was meant to be like study flashcards but I never got around to that part)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct member{      //This emulates a dictionary
    char key[20];   //and links each word+definition pair to the next pair.
    char def[20];
    struct member *ptr;
};
struct member *root;
struct member *curr;
FILE *f;

int fill_list(){                //Fill each member struct with next two lines of
    curr=root;
    char this[20];              //FILE *f
    while(EOF){
        if(!fgets(this,20,f)) break;
        strcpy(curr->key,this);
        if(!fgets(this,20,f)) break;
        strcpy(curr->def,this);
        curr=curr->ptr;
        curr=malloc(sizeof(struct member));
        curr->ptr=0;
    }
    return 0;
}

void free_all(){
    curr=NULL;
    while(curr!=root){
        curr=root;
        while(curr->ptr)curr=curr->ptr;
        free(curr);
    }
}

 int terminate(int i){  //The terminate function closes file and
    free_all(); //frees malloc'd memory, then returns main func's
    fclose(f);  //return value.
    return i;
}

int main(){
    f=fopen("this.txt","r");
    if(!f)return -1;
    root=malloc(sizeof(struct member));
    root->ptr=NULL;
    fill_list();
    curr=root;
    if ( curr != 0 ) {
      while ( curr != 0 ) {
        printf( "%s", curr->key );
        curr = curr->ptr;
      }
    }
    free_all();
    return terminate(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-26T10:04:36+00:00Added an answer on May 26, 2026 at 10:04 am

    Elaborating on Chad’s answer, you could have:

    int fill_list(){                //Fill each member struct with next two lines of
        struct member *new_mem;
        curr=root;
        char this[20];              //FILE *f
        while(1){ /* relying on the breaks below to exit the loop */
            if(!fgets(this,20,f)) break;
            strcpy(curr->key,this);
            if(!fgets(this,20,f)) break;
            strcpy(curr->def,this);
            /* create the new member */
            new_mem=malloc(sizeof(struct member));
            /* set the new member's next ptr to NULL */
            new_mem->ptr = NULL;
            /* set the current member's next to the new member */
            curr->ptr=new_mem;
            /* current is now the new member */
            curr = new_mem;
        }
        return 0;
    }
    

    EDIT: thought I’d just add that if I was going to make slight modifications to your code this is what I’d do. If I were to do it from scratch I wouldn’t structure the loop that way or have unnecessary global variables like curr. Same goes for the point made by sarnold where you only have a single temporary buffer. Not to mention that your last entry in the list could be invalid; it might be a better idea to allocate the next member entry in the list only after you successfully read the two strings into two temporary buffers.

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

Sidebar

Related Questions

I'm just getting myself to familiarize with GIS but i like to know before
I KNOW that this is basic. But I'm so JS-retarded I can't do-it-myself and
I set myself this rather simple sounding challenge but now I am stuck trying
I'm still just getting into MVC, and for my first real project I plan
This is my first time dabbling in NLP so please excuse my ignorance. I'm
So I am pretty new to JQuery and just spent 5 hours getting this
BIG UPDATE: Ok I was getting the whole auto-increment point wrong. I though this
I'm getting this warning for a plugin I'm using: DEPRECATION WARNING: class_inheritable_attribute is deprecated,
Could anyone please explain this to me with an example? I am getting contradicted
I don't do flash myself: in this company, flash is generally used just for

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.