Exchanging words in struct with codex C - c

I am writing a program that will read in two files (hw8.data and codex.data) The hw8.data contains a poem and the codex.data contains words to replace in that poem. I have gotten the files to be loaded and saved in a linked list. I am having trouble finding matched words and replacing them. Also I am having trouble carrying over punctuation from the hw8 on to the newly replaced word for example. hw8: Hello World., codex: World sup, so new word in poem would be Hello sup.
Here is the hw8.data
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
The Codex.data (not all of it)
Eye I
eye I
chequer checker
Pea P
Sea C
plane plainly
lee skip
four for
revue review
Miss Mistakes
Steaks skip
knot not
sea see
quays keys
.
.
.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char * word;
struct node* next;
};
struct codex {
char *word1;
char *word2;
struct codex *next;
};
struct node *loadWords(FILE *stream);
struct codex *loadCodex(FILE *stream);
struct node *exchange(struct node *head, struct codex *headC);
int main()
{
FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
struct codex *headC;
struct codex *tempC;
struct codex *loopC;
head = loadWords(stream);
if (head == NULL) {
return 1;
}
temp = head;//print each word
while (temp != NULL) {
//printf ( "%s\n", temp->word);
temp = temp->next;
}
headC = loadCodex(stream);
if (headC == NULL) {
return 1;
}
tempC = headC;//print each word
while (tempC != NULL) {
//printf ( "%s %s\n", tempC->word1, tempC->word2);
tempC = tempC->next;
}
struct node *exchangedHead;
struct node *temp2;
exchangedHead = exchange(head, headC);
if (exchangedHead == NULL) {
return 1;
}
temp2 = exchangedHead;//print each word
while (temp2 != NULL) {
//printf ( "%s ", temp2->word);
temp2 = temp2->next;
}
temp = head;// free memory
while(temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
tempC = headC;// free memory
while (tempC != NULL) {
loopC = tempC->next;
free ( tempC->word1);
free ( tempC->word2);
free ( tempC);
tempC = loopC;
}
return 0;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
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;
}
return head;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct codex *loadCodex(FILE *stream) {
struct codex *loop = NULL;
struct codex *temp = NULL;
struct codex *head = NULL;
char *words = NULL;
char *currentWord;
char *currentWord2;
size_t chrCount = 0;
if (( stream = fopen("codex.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
currentWord2 = strtok(NULL, "\n");
while (currentWord != NULL && currentWord2 != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word1 = strdup ( currentWord);//allocate memory and copy token to word
temp->word2 = strdup ( currentWord2);
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;
}
return head;
}
struct node *exchange(struct node *head, struct codex *headC) {
struct node *temp;
temp = head;
while(head != NULL && headC != NULL) {
if(strcmp(head->word, headC->word1)) {
head->word = relloc(head->word, strlen(headC->word2));
head->word = headC->word2;
head->next = temp->next;
headC = headC->next;
temp = temp->next;
}
}
}

The issue you are having in exchange (other than misspelling realloc and failing to return a value) is failing to allocate space for the null-terminating character on realloc. (note I haven't checked the list operations for correctness)
head->word = realloc(head->word, strlen(headC->word2));
should be
head->word = realloc(head->word, strlen(headC->word2) + 1);
Which would explain why you are losing the 1-char punctuation off the end of each line.
You should also realloc to a tmp pointer and only assign head->word = tmp; if the reallocation succeeded. (otherwise you lose the original contents of head->word) Also, if you are not going to return a value from exchange, just make the function void (otherwise return head). Let me know if this doesn't correct the issue and I'll dig deeper.
More Issues
Looking at the code, I saw several more issues (some significant). Primarily, your exchange function was a train wreck. Generally, when iterating a linked list, you create a temporary pointer to iterate, rather than using the list address (as you did in main). Why didn't you do that in exchange? Given that, I scrapped exchange and rewrote it. It is included below. In your read functions, wise choice in use of getline, but there is no need to reset chrCount = 0; or words = NULL; in order for getline to allocate the next line. It does it automatically.
However, you committed a foul when you sent the line words to strtok -- without first making a copy. Why? getline allocated the space and needs to manage/free the block of memory it allocated. When you send words to strtok it modifies the memory, inserting null-terminating characters which can lead to stray errors when getline attempts to free/realloc the block. Always make a copy of the line allocated by getline before sending the line to strtok. Additionally, since you must free the copy, create a pointer to your copy so you preserve the start address for your memory block in order to pass to free when you are done.
There is no need to pass an unopened FILE* pointer to your read functions. That can be declared within the function. What you do need to pass is a filename so your read functions to know what to open. I changed main to include its arguments so you now pass the words/codex filenames as arguments.
Note: I just used your sample codex values for my test.
Look over the code at the modifications and the comments:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char * word;
struct node* next;
};
struct codex {
char *word1;
char *word2;
struct codex *next;
};
struct node *loadWords(char *filename);
struct codex *loadCodex(char *filename);
struct node *exchange(struct node *head, struct codex *headC);
int main (int argc, char **argv)
{
if (argc < 3) {
fprintf (stderr, "error: insufficient input. Usage: %s nodefile codexfile\n", argv[0]);
return 1;
}
// FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
struct codex *headC;
struct codex *tempC;
struct codex *loopC;
head = loadWords(argv[1]);
if (head == NULL) {
return 1;
}
printf ("\nOriginal:\n\n");
temp = head;//print each word
while (temp != NULL) {
printf ( " %s", temp->word);
temp = temp->next;
}
printf ("\n");
headC = loadCodex(argv[2]);
if (headC == NULL) {
return 1;
}
tempC = headC;//print each word
while (tempC != NULL) {
// printf ( "%s %s\n", tempC->word1, tempC->word2);
tempC = tempC->next;
}
struct node *exchangedHead;
struct node *temp2;
exchangedHead = exchange(head, headC);
if (exchangedHead == NULL) {
return 1;
}
printf ("\nExchanged:\n\n");
temp2 = exchangedHead;//print each word
while (temp2 != NULL) {
printf ( " %s", temp2->word);
temp2 = temp2->next;
}
printf ("\n");
temp = head;// free memory
while(temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
tempC = headC;// free memory
while (tempC != NULL) {
loopC = tempC->next;
free ( tempC->word1);
free ( tempC->word2);
free ( tempC);
tempC = loopC;
}
return 0;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct node *loadWords(char *filename) {
FILE *stream = NULL;
struct node *loop = NULL;
struct node *temp = NULL;
struct node *head = NULL;
char *words = NULL;
char *currentWord;
size_t chrCount = 0;
ssize_t nchr = 0;
if ((stream = fopen(filename, "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while((nchr = getline (&words, &chrCount, stream)) != -1) {//read a line from file
char *wtemp = strdup (words); /* copy words, strtok will modify */
char *sp = wtemp; /* save wtemp address, pass sp */
currentWord = strtok(sp, " ");//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
}
if (wtemp) free (wtemp);
wtemp = NULL;
// chrCount = 0; //so readline will allocate memory for next line (***NOT REQUIED***)
// words = NULL; (***NOT REQUIED***)
}
if (words) free (words);//release memory
if (stream) fclose (stream);
return head;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct codex *loadCodex(char *filename) {
FILE *stream = NULL;
struct codex *loop = NULL;
struct codex *temp = NULL;
struct codex *head = NULL;
char *words = NULL;
char *currentWord;
char *currentWord2;
size_t chrCount = 0;
ssize_t nchr = 0;
if (( stream = fopen(filename, "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while(getline( &words, &chrCount, stream) > 0) {//read a line from file
while (nchr > 0 && (words[nchr-1] == '\n' || words[nchr-1] == '\r'))
words[--nchr] = 0; /* strip newline or carriage rtn */
char *wtemp = strdup (words); /* copy words, strtok will modify */
char *sp = wtemp; /* save wtemp address, pass sp */
currentWord = strtok(sp, " ");//get first token
currentWord2 = strtok(NULL, "\n");
while (currentWord != NULL && currentWord2 != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word1 = strdup ( currentWord);//allocate memory and copy token to word
temp->word2 = strdup ( currentWord2);
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
}
if (wtemp) free (wtemp);
wtemp = NULL;
// chrCount = 0;//so readline will allocate memory for next line
// words = NULL;
}
if (words) free (words);//release memory
if (stream) fclose (stream);
return head;
}
// struct node *exchange(struct node *head, struct codex *headC) {
// struct node *temp;
// temp = head;
// while(head != NULL && headC != NULL) {
// if(strcmp(head->word, headC->word1)) {
// head->word = realloc(head->word, strlen(headC->word2));
// head->word = headC->word2;
// head->next = temp->next;
// headC = headC->next;
// temp = temp->next;
// }
// }
// }
struct node *exchange(struct node *head, struct codex *headC)
{
struct node *temp = head;
while(temp != NULL)
{
char *wtemp = NULL;
struct codex *tempC = headC;
while (tempC != NULL)
{
if (strcmp(temp->word, tempC->word1) == 0) {
size_t tclen = strlen(tempC->word2);
wtemp = realloc(temp->word, tclen + 1); /* add 1 for null-term */
if (!wtemp) {
fprintf (stderr, "%s() error: realloc failed.\n", __func__);
return head;
}
temp->word = wtemp;
memcpy (temp->word, tempC->word2, tclen + 1); /* copies the null-term also */
}
tempC = tempC->next;
}
temp = temp->next;
}
return head;
}
Output
$ ./bin/ll_single_replace_w dat/ll_replace_poem.txt dat/ll_replace_codex.txt
Original:
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
Exchanged:
I have a spelling chequer,
It came with my P Sea.
It plainly skip marks for my revue,
Mistakes skip I can not sea.
I strike the keys and type a whirred,
And weight for it two say,
Weather I am write oar wrong,
It tells me straight aweigh.
I ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My checker tolled me sew.
A checker is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when I rime.
Each frays come posed up on my screen,
I trussed too bee a joule.
The checker pours over every word,
Two cheque sum spelling rule.

Related

print from users input linked list of struct

I am required to have a list of structs of sentence nodes that point to a struct of word nodes. I am trying to print the user's input.
I have a program that runs properly when I manually give it the input (see test section of the code). It does not, however, work when I use my input1() function.
I've tried debugging it, but I can't seem to find the problem.
I removed all printf lines that I used to debug. I also removed all the irrelevant code.
I am looking to know how to fix it and what is wrong so I can run it with no problems.
What I learned from debugging it is that (only when using input1() and not in the test) the head is overwritten every time and all the nodes as well.
I also tried using a double pointer instead of returning para but that didn't help.
any help will be appreciated,
thanks in advance
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct word
{
char * ch;//poiter to char
}
W;
typedef struct sentence
{
W * currentWord;//pointer to a word
int lineNumber;// holds the line number
int numbersOfWords;//holds the number of words
struct sentence* link;
}
sent;
typedef struct list
{
sent* head;
int count;
}
LISTS;
LISTS* createList()
{
LISTS* list;
list= (LISTS*) malloc (sizeof (LISTS));
if (list)
{
list-> head = NULL;
list-> count = 0;
}
return list;
} // createList
void printList(LISTS* list)
{
sent *temp = list -> head;
//iterate the entire linked list and print the data
while(temp != NULL)
{
printf("%s\n", temp->currentWord->ch);
temp = temp->link;
}
// printf("NULL\n");
}
void insertSentList (LISTS* list, W* itemPtr)
{
sent* newPtr; //new node
if (!(newPtr = (sent * ) malloc(sizeof(sent)))){
printf(" Memory can not be allocated.");
return;
}
newPtr->currentWord = itemPtr;
newPtr->link = NULL;
if(list->head == NULL)
{
list->head = newPtr;
}else{
sent* current = list->head;
while(current->link != NULL){
current = current->link;
}
current -> link = newPtr;
}
(list->count)++;
return;
} // insertList
LISTS * input1(LISTS *para)
{
char * line;
line = (char * ) malloc(1000 * sizeof(char));
line[0] = '\0';
while (line[0] != '\n')
{
W word;
word.ch = (char * ) malloc(100);
printf(" Please input a line : ");
fgets(line, 1000, stdin);
if(line[0] != '\n'){
strcpy(word.ch, line);
insertSentList(para,&word);
}
}
free(line);
return para;
}
int main()
{
///////////////////test////////////////
LISTS* list = createList();
W word;
word.ch= "word0 ";
W word1;
word1.ch= "word1 ";
W word2;
word2.ch= "word2";
insertSentList(list,&word);
insertSentList(list,&word1);
insertSentList(list,&word2);
insertSentList(list,&word);
insertSentList(list,&word1);
insertSentList(list,&word2);
printList(list);
///////////////////test////////////////
LISTS *para = createList();
para= input1(para);
printList(para);
return 0;
}
Main problem with the posted code is that "ownership" of the sent and W objects in a list is not well defined. For example word.ch= "word0 "; in main sets the ch pointer pointing to a string literal (which it does not own), but word.ch = malloc(100); in input1 points it to dynamically allocated memory (which it should own, and remember to free later). Because of this, memory allocations cannot be tracked reliably and, even in the cases where things appear to "work", there are multiple memory leaks. It also breaks when the inserted objects are local variables that do not live for the entire lifetime of the list object.
The simplest (if not necessarily the best or most efficient) solution would be to dynamically allocate all objects that go into the list, make the list own them all, and add a function to cleanup once done. To that end insertSentList could be modified as follows.
void insertSentList (LISTS* list, W* itemPtr)
{
sent* newPtr; //new node
if (!(newPtr = malloc(sizeof(sent)))){
printf(" Memory can not be allocated.\n");
return;
}
W *newItem = malloc(sizeof(W)); // <-- make a deep copy of the `itemPtr` argument
newItem->ch = strdup(itemPtr->ch); // including a copy of the string itself
newPtr->currentWord = newItem; // <-- save the copy in the list, not the argument
newPtr->link = NULL;
if(list->head == NULL)
{
list->head = newPtr;
}else{
sent* current = list->head;
while(current->link != NULL){
current = current->link;
}
current->link = newPtr;
}
list->count++;
} // insertList
For proper cleanup and to avoid memory leaks, the following freeList should be called for each list pointer returned by createList and filled by insertSentList.
void freeList(LISTS *list)
{
sent *temp = list->head;
while(temp != NULL)
{
sent *next = temp->link;
free(temp->currentWord->ch);
free(temp->currentWord);
free(temp);
temp = next;
}
free(list);
}

C - how to get rid of memory leaks?

How can I get rid of memory leaks from for example this function:
void AddData(Data **head, char *sentence, int number) {
Words *words = NULL;
char delimiters[] = " \n\0";
char *token = strtok(sentence, delimiters);
while (token != NULL) {
if (IsAlphabetical(token) == 1) {
char *string = (char *)malloc((strlen(token) + 1) * sizeof(char));
strcpy(string, token);
AddWords(&words, string);
free(string);
}
token = strtok(NULL, delimiters);
}
Data *temp = *head;
Data *newData = (Data *)malloc(sizeof(Data));
newData->lineNumber = number;
newData->words = words;
newData->pNext = NULL;
if (*head == NULL)
*head = newData;
else {
while (temp->pNext != NULL)
temp = temp->pNext;
temp->pNext = newData;
}
}
My personal opinion is the leaks appear because of newData, temp and words variables.
I have a few similar functions that cause the same problem.
I have also function for deleting Data struct, but when I call it at the end of previous function in such way DeleteData(&temp) program will not execute. I think it's because my whole list is deleted.
void DeleteData(Data **head) {
Data *temp = *head;
while (temp->pNext != NULL) {
Data *next = temp->pNext;
DeleteWords(&temp->words);
free(temp);
temp = next;
}
free(temp); /* to check that */
*head = NULL;
}
How can I fix this?
Here are some problems I identified:
The trailing \0 in char delimiters[] = " \n\0"; is useless.
Testing if (IsAlphabetical(token) == 1) might be too restrictive. In C anything non 0 is true so you might test if (IsAlphabetical(token) != 0) or just if (IsAlphabetical(token)).
Why allocate a copy of the string to pass to AddWords(&words, string); and then free(string) thereafter? If Addwords() does not keep the pointer it gets, there is no need to allocate a copy.
Does AddWords() call strtok()? strtok() is non-reentrant, which means that if AddWords() or any other function called in this loop, such as IsAlphabetical() calls strtok(), the context used in the loop will be corrupted. You should use strtok_r() instead.
in function DeleteData, why do you iterate testing while (temp->pNext != NULL)? The last item in the list does not get freed properly, the DeleteWords(&temp->words); is not called. This may cause a memory leak. You should just write:
void DeleteData(Data **head) {
Data *temp = *head;
while (temp != NULL) {
Data *next = temp->pNext;
DeleteWords(&temp->words);
free(temp);
temp = next;
}
*head = NULL;
}

inserting strings into linked list is not working correctly

I have a program that is supposed to take an input file and extract the strings from it and add it to a linked list. I do not think I am adding the strings to the linked list correctly and i cannot seem to find the right way to. When the program executes it goes into an infinite loop for some reason.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
char *string;
struct list *next;
};
typedef struct list LIST;
int main() {
FILE *fp;
char line[128];
char file_name[20];
LIST *current, *head;
char *p, *s;
head = current = NULL;
printf ("Enter the name of the file: ");
scanf("%s",file_name);
fp = fopen(file_name, "r");
while(fgets(line, sizeof(line), fp))
{
p = s = line;
while(*p!=0)
{
if (*p==' ')
{
LIST *node = malloc(sizeof(LIST));
*p = 0;
node->string = strdup(s);
node->next =NULL;
if(head == NULL){
current = head = node;
} else {
current = current->next = node;
}
s = p+1;
}
p++;
}
}
fclose(fp);
//test print
for(current = head; current ; current=current->next){
printf(" %s", current->string);
}
return 0;
}
Several things:
You are using the same node for all words in a line, thereby creating a loop where node->next equals node. You should create a new node when you insert a new word, not when you read a new line.
You don't catch the last word of a line. You can make use of the fact that fgets retains the trailing new-line character and check for that in addition to checking for a space. You could also consider using isspace from <ctype.h>.
Alternatively, and maybe better, is to defer the check for the null terminator until after the loop. You must then add a new word when you read a space or a null character.
You insert empty words when the input file contains consecutive spaces or space characters. Your program should check whether p > s to add only valid words. (Or your program shoul only add valid words when the previously read character wasn't a space.)
You allocate memory for the nodes and the strings. You should free this memory before exiting the program.
Here's your main loop with the fixes described above:
while(fgets(line, sizeof(line), fp))
{
char *p = line;
char *s = line;
do {
if (*p== ' ' || *p == '\n' || *p == '\t' || *p == '\0') {
if (p > s) {
LIST *node = malloc(sizeof(LIST));
*p = 0;
node->string = strdup(s);
node->next = NULL;
if(head == NULL){
head = node;
} else {
current->next = node;
}
current = node;
}
s = p + 1;
}
p++;
}
while (*p != 0);
}

freeing allocated memory generates segmentation fault

I tried reading from text file, and then put every word in list node(and print it afterwards in reverse order).
The program works good, but when trying to free the allocated list nodes, the program crash.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
typedef struct node{
char* word;
struct node* next;
}; typedef struct node* list;
void freeall(list lst){
list temp = NULL;
while (lst)
{
temp = lst->next;
free(lst);
lst = temp;
}
#if 0
if (lst == NULL){ return; }
freeall(lst->next);
free(lst->word);
free(lst);
#endif // 0
}
void deleteAllNodes(list start)
{
while (start != NULL)
{
list temp = start;
free(temp);
start = start->next;
}
}
list createNode(char* buff){
list newnode = (list)malloc(sizeof(list));
assert(newnode);
newnode->next = NULL;
newnode->word = (char*)calloc(strlen(buff), sizeof(char));
assert(newnode->word);
strcpy(newnode->word, buff);
return newnode;
}
void reverse(const char *str) //you don't need to modify your string
{
if (*str != '\0'){ //if the first character is not '\O'
reverse((str + 1)); // call again the function but with +1 in the pointer addr
printf("%c", *str); // then print the character
}
}
void print_reverse(list lst){
if (lst == NULL) return;
print_reverse(lst->next);
reverse(lst->word);
//free(lst->word);
}
list createList(FILE* ifp){
struct node *loop = NULL;
list curr = NULL;
list lst = NULL;
char *word = NULL;
size_t size = 2;
long fpos = 0;
char format[32];
if (ifp == NULL) // open file
perror("Failed to open file \n");
if ((word = malloc(size)) == NULL) // word memory
perror("Failed to allocate memory");
sprintf(format, "%%%us", (unsigned)size - 1); // format for fscanf
while (fscanf(ifp, format, word) == 1) {
while (strlen(word) >= size - 1) { // is buffer full?
size *= 2; // double buff size
printf("** doubling to %u **\n", (unsigned)size);
if ((word = realloc(word, size)) == NULL)
perror("Failed to reallocate memory");
sprintf(format, "%%%us", (unsigned)size - 1);// new format spec
fseek(ifp, fpos, SEEK_SET); // re-read the line
if (fscanf(ifp, format, word) == 0)
perror("Failed to re-read file");
}
curr = createNode(word);
if (lst == NULL){lst = curr;}
else{
loop = lst;
while (loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = curr;
}
fpos = ftell(ifp); // mark file pos
}
free(word);
return lst;
}
int main(int argc, char* argv[]){
assert(argc == 2);
FILE *ifp = fopen(argv[1], "r");
assert(ifp);
list lst = NULL;
lst = (list)malloc(sizeof(list));
lst = createList(ifp);
print_reverse(lst);
fclose(ifp);
//freeall(lst);
//deleteAllNodes(lst);
return 1;
}
your delete all nodes has a bug in it. You freed a pointer and tried accessing it immediately. So the program crashes You can try this
void deleteAllNodes(list head)
{
list ptr = head;
while ((ptr = head) != NULL)
{
head = head->next;
free (ptr);
}
}
point the current ptr to the head and point head to next element. Delete the current pointer.
In your deleteAllNodes function you are free-ing a pointer and then accessing it. You could try deleting nodes in reverse order, starting from the last one, for instance with a recursive function.
void deleteAllNodes(list start)
{
if (start != NULL)
{
deleteAllNodes(start->next);
free(start);
}
}
Or you can stick to the iterative forward deletion with something like (untested):
void deleteAllNodes(list start)
{
list previous = NULL;
while (start != NULL)
{
if (previous != NULL)
free(previous);
previous = start;
start = start->next;
}
if (previous != NULL)
free(previous);
}
The problem , as I see it is with
list newnode = (list)malloc(sizeof(list));
your list is a typedef to struct node*, so this statement is essentially
list newnode = (list)malloc(sizeof(struct node*));
which is wrong. You're allocating memory for a pointer to structure variable, whereas, you should be allocating memory equal to the size of the structure variable itself.
Two things to mention here
Please see why not to cast the return value of malloc() and family in C.
Never use a typedef for a pointer type. It's not a "rule", but better to stick to it.
Your allocation statement, at least, shall look like
list = malloc(sizeof*list);
Apart from this, in your main() function,
First, you're allocating memory to lst using malloc() [Same issue with the allocation as above]
Then, you assign another pointer, the return value of createList() to lst.
This way, you're overwriting the allocated mekory through malloc(), creating memory leak. You don't need malloc() there, at all.

error with pointers and linked list node creation

I'm trying to read line input from a file, correctly parse the line, and add the three fields of information from the line onto a node in a linked list.
Here's my read from file function:
int readFile(char* file)
{
FILE *fp = fopen(file,"r");
char ch;
char line[50];
char string1[100];
char string2[100];
char string3[100];
char endLine[2];
int i = 0;
while(fscanf(fp, "%[^\t]\t%[^\t]\t%[^\n]", string1, string2, string3) == 3)
{
printf( "%s\t%s\t%s\n", string1, string2, string3);
addNode(string1, string2, string3, head, tail);
}
printNodes();
fclose(fp);
return 0;
}
And here is my addNode function:
// create stuff
Entry *entry = malloc(sizeof(Entry));
entry->name = string1;
entry->address = string2;
entry->number = string3;
Node* node = malloc(sizeof(Node));
node->entry = entry;
node->next = NULL;
// Empty list
if(head->next == NULL)
{
head->next = node;
}
// Else, add to the end of the list
else
{
Node* temp = head->next;
while(temp->next!= NULL)
{
temp = temp->next;
}
temp->next = node;
}
I get problems when I call printNodes, and only the last read node's information is printed X times, where X is the number of unique nodes I'm supposed to have. I think I'm having a problem where I'm overwriting an old node each time I create a new node, but I'm not entirely sure, as this is my first time with raw C code.
Thanks again!
EDIT:
here's the printNodes() function:
int printNodes(Node* head)
{
Node *temp = head->next;
while(temp->next != NULL)
{
printf("\n%s\t%s\t%s\n", temp->entry->name, temp->entry->address, temp->entry->number);
temp = temp->next;
}
return 0;
}
Your problem is here:
entry->name = string1;
entry->address = string2;
entry->number = string3;
You are providing the same memory location to every node. Those strings contain the last value you read in when you call printNodes().

Resources