Question

Urgent please! This is in C.

I need to write a function that reads a .txt file in using fgets (line by line) and makes a linked list, with each node storing a pointer to a char array that stores one word in the line it reads. A "word" can be alphabetical, numerical, punctuation, blank lines, or \n.

I have a struct:

struct node
{
void *data;
struct node *next;
};

that stores a pointer to an array of chars in memory, and a pointer to the next word in the line.

The function that should do this is passed the file pointer and should return a pointer to the first node in the linked list.

The text I'm using to test this is a sample from Franz Kafka's Metamorphosis, heres a few paragraphs:

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi. I have answered similar question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

//structure to represent a node

struct node{

                void* data;

                struct node *next;

};

//required method to read words from a file and create a linked list, return the head

//of the list

struct node* txt2words(FILE *fp){

                //initializing two node pointers to NULL

                struct node* head=NULL;

                struct node* current=NULL;

                //a string to read one line at a time

                char line[200];

                //looping as long as a line is successfully read from input file

                while(fgets(line,200,fp)!=NULL){

                                //tokenizing line by white space to split line into separate words

                                char *word=strtok(line, " ");

                                //looping as long as word is not NULL

                                while(word!=NULL){

                                               //checking if head is NULL

                                               if(head==NULL){

                                                               //assigning memory for head pointer

                                                               head=(struct node*) malloc(sizeof(struct node));

                                                               //assigning memory for data field

                                                               head->data=(char*) malloc(sizeof(20*sizeof(char)));

                                                               //copying word to data field

                                                               strcpy(head->data,word);

                                                               //setting next to NULL

                                                               head->next=NULL;

                                                               //setting head as current node

                                                               current=head;

                                               }else{

                                                               //creating a new node, assigning values

                                                               struct node* node=(struct node*) malloc(sizeof(struct node));

                                                               node->data=(char*) malloc(sizeof(20*sizeof(char)));

                                                               strcpy(node->data,word);

                                                               node->next=NULL;

                                                               //setting as next of current node

                                                               current->next=node;

                                                               //setting this as the new current node

                                                               current=node;

                                               }

                                               //getting next word from the line

                                               word=strtok(NULL, " ");

                                }

                               

                }

                //returning a pointer to the head

                return head;

}

//main for testing

int main(){

                //asking and storing the name of input file

                printf("Enter name of input file: ");

                char filename[20];

                scanf("%s",&filename);

                //opening file, printing error if file not found

                FILE* fp=fopen(filename,"r");

                if(fp==NULL){

                                printf("Error! File not found!\n");

                                return 0; //end of program

                }

                //reading text from file storing the head of linked list in a node pointer

                struct node* head=txt2words(fp);

                //closing file

                fclose(fp);

               

                //taking a reference to head node

                struct node* temp=head;

                //looping until temp is null

                while(temp!=NULL){

                                //printing data stored in temp one word per line

                                printf("%s\n", temp->data);

                                //moving to next node

                                temp=temp->next;

                               

                }

                //line break

                printf("\n");

               

                //now before we exit the program ,we need to deallocate the memory allocated for the

                //nodes, otherwise, it will result in memory leaks

                temp=head;

                while(temp!=NULL){

                                //storing next node (if any)

                                struct node* next=temp->next;

                                //deleting node temp

                                free(temp);

                                //restoring next node as new temp

                                temp=next;

                }

                return 0;

}

Add a comment
Know the answer?
Add Answer to:
Urgent please! This is in C. I need to write a function that reads a .txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C/C++ program that reads a list of words and prints them in alphabetical order...

    Write a C/C++ program that reads a list of words and prints them in alphabetical order with one word per line. If two words have the same beginning sequence, the shorter word should be printed first. If the program is called with an “r” on the command line, the list of words should be printed in reverse alphabetical order. Test your program using the bunch-of-words.txt file bunch-of-words.txt: To Sherlock Holmes she is always the woman. I have seldom heard him...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • i really need help with the graphs Driving Can Be Dangerous to Your Health: An Interrupted...

    i really need help with the graphs Driving Can Be Dangerous to Your Health: An Interrupted Case Study in Physiology Phil Stephens Department of Biology Villanova University Part 1-The Grandparents Arrive Dave pulled the cell phone out of his pocket, cursing himself for not putting it on vibrate. The children, Jason and Laura, were both asleep, and he knew that the rest of the day would not be fun if they were awakened from their naps. "Hi, Dave. We're just...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT