(C) parsing std in into a linked list, infinite loop - c

For some reason the code keeps segfaulting after the first printf at the for loop near the end. It doesnt print the 2nd printf at all, can someone point me in the right direction? I think it has to do with a problem in memory
struct node{
char *fullString;
char fileName[1024];
int lineNumber;
char filler[1024];
struct node *next;
};
int main (int argc, char *argv[])
{
char tmpstring[1024];
/* This won't change, or we would lose the list in memory */
struct node *head = NULL;
//tail
struct node *tail = NULL;
/* This will point to each node as it traverses the list */
struct node *ptr, *newnode;
while (fgets(tmpstring, 1024, stdin) == tmpstring)
{
if ((newnode = (struct node *)malloc(sizeof(struct node))) == NULL) {
fprintf(stderr,"mklist: malloc failure for newnode\n");
exit(1);
}
if ((newnode->fullString = malloc(strlen(tmpstring)+1)) == NULL) {
fprintf(stderr,"mklist: malloc failure for newnode->str\n");
exit(1);
}
if (strncpy(newnode->fullString, tmpstring, strlen(tmpstring)+1) != newnode->fullString) {
fprintf(stderr,"mklist: string copy problem\n");
exit(1);
}
strcpy(newnode->fullString, tmpstring);
newnode->next = NULL;
// if (3 != scanf(tmpstring, "%255s:%d:%255s", newnode->fileName, &newnode->lineNumber, newnode->filler))
// abort();
if (tail == NULL)
head = tail = newnode;
else {
tail->next = newnode;
tail = newnode;
}
}
// Now print the list, element by element.
for (ptr = head; ptr != NULL; ptr = ptr->next)
printf("%s\n", ptr->fullString);
printf("%s:%d:%s\n", ptr->fileName, ptr->lineNumber, ptr->filler);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_LINE_SIZE 1024
struct node
{
char fullString[MAX_LINE_SIZE];
char fileName[MAX_LINE_SIZE];
int lineNumber;
char filler[MAX_LINE_SIZE];
struct node *next;
};
int main (int argc, char **argv)
{
char tmpstring[MAX_LINE_SIZE];
struct node *head = NULL;
struct node *tail = NULL;
struct node *ptr, *newnode;
while (fgets(tmpstring, MAX_LINE_SIZE, stdin))
{
size_t len = strlen(tmpstring);
char *cptr = tmpstring, *eptr;
if (0 == len || tmpstring[len - 1] != '\n')
assert(0), abort(); /* NOTE: will abort on too long lines and if EOF occurs mid-line */
if (NULL == (newnode = calloc(1, sizeof(struct node))))
assert(0), abort();
strcpy(newnode->fullString, tmpstring);
newnode->lineNumber = -1;
newnode->next = NULL;
if (NULL == (eptr = strchr(cptr, ':')))
assert(0), abort();
*eptr = '\0';
if (1 != sscanf(cptr, "%255s", newnode->fileName))
assert(0), abort();
cptr = eptr + 1;
if (NULL == (eptr = strchr(cptr, ':')))
assert(0), abort();
*eptr = '\0';
if (1 != sscanf(cptr, "%d", &newnode->lineNumber))
assert(0), abort();
cptr = eptr + 1;
strcpy(newnode->filler, cptr);
if (tail == NULL)
head = tail = newnode;
else {
tail->next = newnode;
tail = newnode;
}
}
for (ptr = head; ptr != NULL; ptr = ptr->next)
printf("%s:%d:%s", ptr->fileName, ptr->lineNumber, ptr->filler);
return 0;
}
Add some better error handling and that's probably what you were after -- so long as your input doesn't have lines longer than MAX_LINE_SIZE-1 characters.

Related

I am trying to load words from a text file into a binary search tree

I am trying to load words from a text file into a binary search tree.
Each line of the file contains one word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(char *key)
{
node *n = malloc(sizeof(node));
n->key = malloc(sizeof(key));
strcpy(n->key, key);
n->right = n->left = NULL;
return n;
}
node *insirtNode(node *root, char *key)
{
if (!root)
return newnode(key);
if (strcmp(key, root->key) < 0)
root->left = insirtNode(root->left, key);
else if (strcmp(key, root->key) > 0)
root->right = insirtNode(root->right, key);
return root;
}
void readFromFileToTree(const char *fname, node **root)
{
// read each line
FILE *s = fopen(fname, "r");
if (!s) {
printf("101");
exit(0);
}
char *t = NULL;
do {
char temp[100];
t = fgets(temp, 40, s); // printf("%s ",temp); to check what words are being insirting and whats not
*root = (node *)insirtNode(*root, temp);
} while (t);
fclose(s);
}
int main()
{
node *root = NULL;
readFromFileToTree("anas.txt", &root);
return 0;
}
the code stops before reading every word from the file
and returns -1073740940
The memory size allocated for the word in newnode() is incorrect: n->key = malloc(sizeof(key)); allocates the size of a pointer, not the length of the string plus an extra byte for the null terminator.
You should use strdup() that allocates and copies the string in a single call:
node *newnode(const char *key) {
node *n = malloc(sizeof(node));
n->key = strdup(key);
n->right = n->left = NULL;
return n;
}
Note also these problems:
printf("101"); is a cryptic error message. You should use
fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
and exit with a non-zero status;
the loop in readFromFileToTree() is incorrect: you will pass a null pointer to insirtNode() at the end of file. You should use this instead:
char temp[100];
while(fgets(temp, sizeof temp, s)) {
*root = insirtNode(*root, temp);
}
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(const char *key) {
node *n = malloc(sizeof(*n));
n->right = n->left = NULL;
n->key = strdup(key);
return n;
}
node *insertNode(node *root, const char *key) {
if (!root)
return newnode(key);
int cmp = strcmp(key, root->key);
if (cmp < 0)
root->left = insertNode(root->left, key);
else
if (cmp > 0)
root->right = insertNode(root->right, key);
return root;
}
node *readFromFileToTree(const char *fname) {
node *root = NULL;
FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
return NULL;
}
char temp[100];
while (fgets(temp, sizeof temp, fp)) {
root = insertNode(root, temp);
}
fclose(fp);
return root;
}
void freeTree(node *n) {
if (n) {
freeTree(n->left);
freeTree(n->right);
free(n);
}
}
int main() {
node *root = readFromFileToTree("anas.txt");
freeTree(root);
return 0;
}

Segmentation fault for Loop vs While Loop to iterate over Linked list in C

After reviewing the information regarding the linked list (still trying to make my head around the topic). I rewrote the following code, and this is the final version: (the code aims to prompt for a directory, and a wildcard, to create a linked list of files found in that directory with this file extension.
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct nlist{
char *data;
struct nlist *next;
}Node;
Node* insert(Node*, char*);
void show(Node*);
Node* insert(Node *Head, char *value)
{
Node *new_string;
new_string = (Node *)malloc(sizeof(Node));
new_string->data = malloc(strlen(value)+1);
strcpy(new_string->data,value);
Node *check;
check = (Node *)malloc(sizeof(Node));
if(Head == NULL){
Head = new_string;
Head->next = NULL;
}
else{
check = Head;
while(check->next != NULL)
check = check->next;
check->next = new_string;
new_string->next = NULL;
}
return Head;
}
void show(Node *Head)
{
Node *check;
check = (Node *)malloc(sizeof(Node));
check = Head;
if (check == NULL){
return;
}
while(check != NULL) {
printf("%s", check->data);
check=check->next;
}
printf("\n");
}
void listFilesRecursively(char *path, char *suffix);
int main()
{
char path[100];
char suffix[100];
// Input path from user
// Suffix Band Sentinel-2 of Type B02_10m.tif
printf("Enter path to list files: ");
scanf("%s", path);
printf("Enter the wildcard: ");
scanf("%s", suffix);
listFilesRecursively(path, suffix);
return 0;
}
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
void listFilesRecursively(char *basePath, char *suffix)
{
char path[1000];
struct dirent *dp;
DIR *dir = opendir(basePath);
Node *Head = NULL;
if (!dir)
return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if (string_ends_with(path, suffix))
Head = insert(Head, path);
listFilesRecursively(path, suffix);
}
}
//show(Head);
/*
Node *check;
check = (Node *)malloc(sizeof(Node));
check = Head;
if (check == NULL){
return;
}
while(check != NULL) {
printf("%s", check->data);
//open_array(check->data);
check=check->next;
}
printf("\n");
//return Head;
*/
Node *p;
p = (Node *)malloc(sizeof(Node));
for (p = &Head; p != NULL; p = p->next){
printf(stdout, "Data: %s\n", p->data);
}
closedir(dir);
}
My problem:
I commented the while loop in which I am able to print out the Nodes Data that I chained when
using the insert(Head, path) function. However, When I am using a for loop to do the same
Node *p // to create a Node pointer
p = (Node*)malloc(sizeof(Node)); // to allocate space for that node
for (p = &Head; p!= NULL; p = p->next){
printf(stdout, "Data: %s\n", p->data); // to print the nodes in the for loop starting by getting the address of the Head of the linked list
}
Why do I incur in a segmentation fault? Is the iteration over a linked list possible using a for loop, the same as using a while loop in C?
Node *p;
if (!(p = (Node*)malloc(sizeof(Node))))
return;
for (p = Head; p != NULL; p = p->next){
printf(stdout, "Data: %s\n", p->data);
}
No need to reference to your Head's address as it is already one.
Protecting your programs against malloc fails is also a good habit.

how to read multiple lines from stdin and store it in linked list

I'm trying to write a program that get's lines from the user (from STDIN) and store them in a linked list.
right now I'm getting only one line and terminate the program.
how can I change the code to keep getting lines from stdin?
also, if someone can tell me if I'm allocating and freeing memory as it should be it will be very helpful.
Thanks.
#include <stdio.h>
#include <stdlib.h>
int BUFF_SIZE = 128;
struct Node {
char* data;
struct Node* next;
};
struct Node* head = NULL;
struct Node* tail = NULL;
void free_list(struct Node* head)
{
if (head != NULL)
{
free_list(head->next);
free(head);
}
}
int main()
{
int curr_size = 0;
char* pStr = malloc(BUFF_SIZE);
curr_size = BUFF_SIZE;
printf("%s", "please print multiple lines\n");
if (pStr != NULL)
{
char c;
int i = 0;
while ((c = getchar()) != '\n' && c != EOF)
{
pStr[i++] = c;
if (i == curr_size)
{
curr_size = i + BUFF_SIZE;
pStr = realloc(pStr, curr_size);
if (pStr == NULL) return;
}
}
pStr[i] = '\0';
struct Node* new_node = malloc(sizeof(struct Node*));
char* new_data = malloc(sizeof(pStr));
new_data = pStr;
new_node->data = new_data;
if (head == NULL)
{
head = new_node;
tail = new_node;
}
else
{
tail->next = new_node;
}
}
free_list(head);
}
Few problems:
As of now you are terminating the reading upon reacieving the \n.
if (pStr == NULL) return; //error
int c;
int i = 0;
while ((c = getchar()) != EOF)
{
/*New word, insert into linked list*/
if (c == '\n'){
pStr[i] = '\0';
struct Node* new_node = malloc(sizeof(*new_node));
char* new_data = malloc(i+1);
strcpy(new_data, pStr);
new_node->data = new_data;
if (head == NULL)
{
head = new_node;
tail = new_node;
}
else
{
tail->next = new_node;
tail = new_node;
}
i = 0; //Reset the index
}
else {
pStr[i++] = c;
if (i == curr_size)
{
curr_size = i + BUFF_SIZE;
pStr = realloc(pStr, curr_size);
if (pStr == NULL) return;
}
}
}
Memory leaks and node data will be always pointing to latest content of pStr.
char* new_data = malloc(sizeof(pStr));
new_data = pStr; //Memory leak here
new_node->data = new_data;
change it to
char* new_data = malloc(i+1);
strcpy(new_data, pStr);
new_node->data = new_data;
sizeof(pStr) is size of pointer not the string length.
You need to update tail after each node is inserted to list.
else
{
tail->next = new_node;
}
to
else
{
tail->next = new_node;
tail = new_node;
}

Not reading number properly?

I'm trying to read from file line by line. It takes the first number of the line and the rest it connects it using a char of linked list. But when I run it, i get the connection as -38 (which is wrong) and it only prints it once and does not go through the rest of the line. but it reads first element perfectly.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
int main(void)
{
FILE * fp;
char * line = NULL;
char * storage;
size_t len = 0;
ssize_t read;
struct node *G[1000];
for (int i = 1; i < 1000; i++)
{
G[i]= malloc(sizeof(struct node));
G[i]->data = i;
G[i]->next = NULL;
}
fp = fopen("idk2.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
int vertexGettingConntected = line[0];
struct node* newItem;
printf("Retrieved line of length %zu :", read);
int i = 0;
while(line[i] != '\0'){
if ( line[i] == ' '){
i++;
}
else if (i == 0){
newItem = malloc(sizeof(struct node));
int itemStorage = line[0] - '0';
newItem->next = NULL;
newItem->data = itemStorage;
G[itemStorage] = newItem;
printf("This is first Number:%d\n", itemStorage);
}
else if (line[i] != ' '){
struct node* addingItem = newItem;
while(addingItem->next != NULL){
addingItem = addingItem->next;
}
int itemStorage = line[i] - '0';
struct node* newConnection = malloc(sizeof(struct node));
addingItem->next = newConnection;
newConnection->data = itemStorage;
newConnection->next = NULL;
printf("This is character:%c\n", line[i]);
printf("This is connection:%i\n", itemStorage);
}
i++;
}
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
for(int printer = 1; printer<20; printer++){
printf("%d\n",G[printer]->data);
}
}
EDIT:
Just wanted to include file im reading from:
1 3 4
2 4
3 1 4
4 2 1 3
I am not sure why you want to have an array of pointers to node which you all allocate memory for at the beginning of your program without knowing how many nodes will be needed. Then you allocate memory again while reading from the file.
Given the constraints of not being allowed to use functions, thats how i'd read the file and build a list:
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;
int main(void)
{
char const *filename = "test.txt";
FILE *input_file = fopen(filename, "r");
if (!input_file) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", filename);
return EXIT_FAILURE;
}
// read from file and build a list:
node_t *head = NULL;
node_t *tail = NULL;
int value;
int result = EXIT_SUCCESS;
while (fscanf(input_file, "%d", &value) == 1) {
node_t *new_node = calloc(1, sizeof *new_node);
if (!new_node) {
fputs("Not enough memory :(\n\n", stderr);
result = EXIT_FAILURE;
goto glean_up;
}
new_node->value = value;
if (!head) { // there is no head yet so new_node becomes the head
head = tail = new_node; // which is also the lists tail
continue;
}
tail->next = new_node;
tail = new_node;
}
// print the list:
for (node_t *current_node = head; current_node; current_node = current_node->next)
printf("%d ", current_node->value);
putchar('\n');
clean_up:
fclose(input_file);
for (node_t *current_node = head, *temp; current_node; current_node = temp) {
temp = current_node->next;
free(current_node);
}
return result;
}
Ideally you'd write functions to manage the list:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;
typedef struct list_tag {
node_t *head;
node_t *tail;
} list_t;
void list_create(list_t *list)
{
list->head = list->tail = NULL;
}
bool list_push_back(list_t *list, int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if (!new_node)
return false;
new_node->value = value;
if (!list->head) {
list->head = list->tail = new_node;
return true;
}
list->tail->next = new_node;
list->tail = new_node;
return true;
}
void list_print(list_t *list)
{
for (node_t *current_node = list->head; current_node; current_node = current_node->next)
printf("%d ", current_node->value);
}
void list_free(list_t *list)
{
for (node_t *current_node = list->head, *temp; current_node; current_node = temp) {
temp = current_node->next;
free(current_node);
}
}
bool read_int(FILE *input_file, int *value)
{
return fscanf(input_file, "%d", value) == 1;
}
int main(void)
{
char const *filename = "test.txt";
FILE *input_file = fopen(filename, "r");
if (!input_file) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", filename);
return EXIT_FAILURE;
}
// read from file and build a list:
list_t list;
list_create(&list);
int value;
while (read_int(input_file, &value)) {
if (!list_push_back(&list, value)) {
fputs("Not enough memory :(\n\n", stderr);
// clean up:
fclose(input_file);
list_free(&list);
return EXIT_FAILURE;
}
}
// print the list:
list_print(&list);
putchar('\n');
// clean up:
fclose(input_file);
list_free(&list);
}
Output:
1 3 4 2 4 3 1 4 4 2 1 3
When you hit a space character you iterate i. At the end of your loop you iterate i.
So when you hit a space character you iterate i twice, skipping the number and landing on the next space character.
Which is why you get the first number but miss the rest.
edit: removed getline() comments due to feedback from #swordfish. Current implementation is not problematic.

Reading words from file into dynamic char with linked lists

I am trying to read data from a file and save the data into a linked list. We are not able to make the char word into a static char. We have to make it dynamic to accept a word of any length using char *word. I am having trouble reading the words from the file and saving it into the dynamic char. I have done this before with static char which is easy. Here is the code.
#include <stdio.h>
#include <stdlib.h>
struct node {
char *word;
struct node *next;
};
struct codex {
char *word;
struct codex *next;
};
struct node *loadWords(FILE *stream);
int main() {
struct node *head;
FILE *stream;
head = loadWords(stream);
return 0;
}
struct node *loadWords(FILE *stream) {
struct node *poem;
struct node *temp;
char *words, *currentWord;
size_t chrCount;
stream = fopen("hw8.data", "r");
rewind(stream);
while(!feof(stream)) {
if(temp = (struct node*)calloc(1, sizeof(struct node)) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
getline(&words, &chrCount, stream);
currentWord = strtok(words, " ");
strcpy(temp->word, words);
head->next = temp;
head = head->next;
}
return poem;
}
How am I to do this dynamically?
This uses getline to read each line from the file. If words is set to NULL and chrCount is set to zero, getline will allocate the memory needed to store the line from the file.
As the line is tokenized, a structure is calloc'd. strdup will allocate the memory to store the token and copy the token into the structure.
The new structure is added to the end of the linked list. The memory allocated for words is released and words and chrCount are reset for the next line.
The loop will continue as long as getline returns a value greater than 0.
In main, the list is traversed printing each word and then traversed again, freeing the allocated memory.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char * word;
struct node* next;
};
struct node *loadWords(FILE *stream);
int main()
{
FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
head = loadWords(stream);
if ( head == NULL) {
return 1;
}
temp = head;//print each word
while ( temp != NULL) {
printf ( "%s\n", temp->word);
temp = temp->next;
}
temp = head;// free memory
while ( temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
return 0;
}
struct node *loadWords(FILE *stream) {
struct node *loop = NULL;
struct node *temp = NULL;
struct node *head = NULL;
char *words = NULL;
char *currentWord;
size_t chrCount = 0;
if ( ( stream = fopen("hw8.data", "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while( getline( &words, &chrCount, stream) > 0) {//read a line from file
currentWord = strtok(words, " ");//get first token
while ( currentWord != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word = strdup ( currentWord);//allocate memory and copy token to word
if ( head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
free ( words);//release memory
chrCount = 0;//so readline will allocate memory for next line
words = NULL;
}
fclose ( stream);
return head;
}
Since you seem to know how to allocate memory, I'll assume the issue is that you don't know how much to allocate for each word. (Similar ideas apply for reading in lines).
If you had some idea about about how big each word could possibly be, you could statically allocate that, and then once each word has been read in, dynamically allocate the correct size you need.
Otherwise, you could read in your text one character at a time until you have finished a word, so that you can increase your buffer as needed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *word;
struct node *next;
};
//struct codex unused
struct node *loadWords(FILE *stream);
int main(void) {
struct node *head;
FILE *stream = fopen("hw8.data", "r");//No sense to pass arguments If you do not open at caller side
head = loadWords(stream);
fclose(stream);
{//test print and release
struct node *p = head;
while(p){
struct node *temp = p->next;
puts(p->word);
free(p->word);
free(p);
p = temp;
}
}
return 0;
}
struct node *loadWords(FILE *stream) {
struct node *head = NULL, *curr, *temp;
char *words = NULL, *currentWord;
size_t chrCount = 0;
rewind(stream);
while(getline(&words, &chrCount, stream) != -1){
currentWord = strtok(words, " \t\n");
while(currentWord != NULL){
if((temp = calloc(1, sizeof(struct node))) == NULL) {
fprintf(stderr, "ERROR - Could not allocate memory.\n");
exit(EXIT_FAILURE);
}
temp->word = strdup(currentWord);//malloc and strcpy
if(head == NULL){
curr = head = temp;
} else {
curr = curr->next = temp;
}
currentWord = strtok(NULL, " \t\n");
}
}
free(words);
return head;
}
Explaining the idea :
My idea is to link characters of each word in a linked list named Word . Then , i put each word in another linked list named Node . ( And of course everything will be allocated dynamically ).
An example of text :
Hello World, here.
Our algorithm will do something like this :
H->e->l->l->o -> W->o->r->l->d -> h->e->r->e
Note : words are separated by either space or standard punctuation , so i used isspace and ispunct functions .
Here is my code :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <ctype.h>
struct Node {
char * word;
struct Node* next;
};
struct Word {
char chr;
struct Word* next;
};
int main (void)
{
static const char filename[] = "C:\\a.txt";
FILE *file = fopen(filename, "r");
Node *list = 0;
Node **last = &list;
Word *word = 0;
int list_size = 0;
int word_size = 0;
if ( file != NULL )
{
int ch, inword = 0;
while ( 1 )
{
ch = fgetc(file);
if ( isspace(ch) || ispunct(ch) || ch == EOF )
{
if ( inword )
{
inword = 0;
char * string = (char *) malloc(word_size+1);
for(int i=word_size-1; i>=0; i--) {
string[i]= word->chr;
Word * aux = word;
word = word->next;
free(aux);
}
string[word_size] = '\0';
Node * aux = (Node *) malloc(sizeof(Node));
aux->word = string;
aux->next = 0;
*last = aux;
last = & aux->next;
word_size = 0;
}
if(ch == EOF)
break;
}
else
{
inword = 1;
Word * aux = word;
word = (Word *) malloc(sizeof(Word));
word->chr = ch;
word->next = aux;
word_size++;
}
}
fclose(file);
for(Node * aux = list ; aux; aux=aux->next) {
puts(aux->word);
}
getchar();
}
return 0;
}
Hope I helped .
Happy Coding :D

Resources