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.
Related
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;
}
I would like to save an array of linked lists in a binary file, but I don't know how to assign the dynamic memory due to the varying lengths of linked lists for each bucket.
And to access a random position containing the whole linked list without reading the whole file, like a kind of index file?
Some tip?
I will make these assumptions:
Every (non-“null”) element of each list is an int
There are MAX_INT or fewer elements in each list
Make your file (remember to open in binary mode) have the following structure:
Output an int containing the number of items in the first list
Output each int in that list
Repeat for each subsequent list
This scheme has two drawbacks:
each list has a (large) maximum number of elements
you must traverse each list twice to save (first to learn the length, again to save each item)
It has the advantage of being very quick and direct to load.
here is a simple code,you can use fwrite to file and fread from file as you wish,but you must consider the big-endian or little-endian
#include <stdio.h>
#include <stdlib.h>
typedef struct _Node {
struct _Node *next;
int val;
} Node;
void serialize(Node arr[], int size[], int len) {
FILE *f = fopen("serialize.bin", "wb");
//write arr len
fwrite(&len, sizeof(int), 1, f);
//write array
for (int i = 0; i < len; ++i) {
//write linklist len
fwrite(&size[i], sizeof(int), 1, f);
Node *tmp = &arr[i];
while (tmp) {
fwrite(&tmp->val, sizeof(int), 1, f);
tmp = tmp->next;
}
}
fclose(f);
}
Node *deserialize() {
FILE *f = fopen("serialize.bin", "rb");
int len;
fread(&len, sizeof(int), 1, f);
Node *ret = malloc(len * sizeof(Node));
for (int i = 0; i < len; ++i) {
int linklen;
int val;
Node *tmp = &ret[i];
fread(&linklen, sizeof(int), 1, f);
fread(&val, sizeof(int), 1, f);
ret[i].val = val;
ret[i].next = NULL;
linklen--;
while (linklen--) {
fread(&val, sizeof(int), 1, f);
tmp->next = malloc(sizeof(Node));
tmp->next->next = NULL;
tmp->next->val = val;
tmp = tmp->next;
}
}
return ret;
}
int main() {
#define ARRAY_LEN 3
Node arr[ARRAY_LEN];
int size[ARRAY_LEN];
arr[0].next = malloc(sizeof(Node));
arr[0].val = 1;
arr[0].next->next = malloc(sizeof(Node));
arr[0].next->val = 2;
arr[0].next->next->next = NULL;
arr[0].next->next->val = 3;
size[0] = 3;
arr[1].next = malloc(sizeof(Node));
arr[1].val = 11;
arr[1].next->next = NULL;
arr[1].next->val = 22;
size[1] = 2;
arr[2].next = NULL;
arr[2].val = 33;
size[2] = 1;
serialize(arr, size, ARRAY_LEN);
Node *ret = deserialize();
//TODO free memory
return 0; //end of code
}
The solution is not very complicated if there is a value that is never stored as an element in your linked list-- in that case, you can treat the linked list like a glorified null-terminated string, where whatever value that cannot exist in the linked list acts as the terminator. If you don't have such luxury, then you can write an extra byte with each value that determines if the value is the last one or not. Here's an implementation of the first possible solution, though it can be easily modified to work in the case of the second:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NODE_V_SIZE sizeof(int)
const int list_end = -1;
typedef struct Node {
int value;
struct Node *prev, *next;
} Node;
void free_nodes(Node *head) {
while (head) {
Node *to_free = head;
head = head->next;
free(to_free);
}
}
Node* new_node(int value) {
Node *node = malloc(sizeof(Node));
if (!node) {
return NULL;
}
node->prev = NULL;
node->next = NULL;
node->value = value;
return node;
}
int write_nodes(int fd, Node *head) {
while (head) {
if (write(fd, &head->value, NODE_V_SIZE) != NODE_V_SIZE) {
return -1;
}
head = head->next;
}
if (write(fd, &list_end, NODE_V_SIZE) != NODE_V_SIZE) {
return -1;
}
return 0;
}
Node* read_nodes(int fd) {
Node *head = NULL;
Node *cur = NULL;
int in;
while (1) {
if (read(fd, &in, NODE_V_SIZE) != NODE_V_SIZE) {
free_nodes(head);
return NULL;
}
if (in == list_end) {
return head;
}
if (cur) {
cur->next = new_node(in);
if (!cur->next) {
free_nodes(head);
return NULL;
}
cur->next->prev = cur;
cur = cur->next;
} else {
cur = new_node(in);
if (!cur) {
return NULL;
}
head = cur;
}
}
}
Node* new_random_list(int modifier) {
Node *head = new_node(0);
if (!head) {
printf("out of memory\n");
exit(1);
}
Node *cur = head;
for (int i = 0; i < 10; i++) {
cur->next = new_node((i+modifier)*3);
if (!cur->next) {
free_nodes(head);
printf("out of memory\n");
exit(1);
}
cur = cur->next;
}
return head;
}
int main() {
#define NHEADS 10
// open file
int fd = open("dat", O_RDWR);
if (fd == -1) {
printf("failed to open file\n");
exit(1);
}
// make NHEADS number of 10 nodes linked together and write them to fd
for (int i = 0; i < NHEADS; i++) {
Node *head = new_random_list(i);
if (write_nodes(fd, head)) {
printf("failed to serialize nodes\n");
exit(1);
}
free_nodes(head);
}
// read from fd to populate got then print it, repeat NHEADS times
lseek(fd, 0, SEEK_SET);
for (int i = 0; i < NHEADS; i++) {
Node *got = read_nodes(fd);
if (!got) {
printf("failed to deserialize nodes\n");
exit(1);
}
Node *head = got;
while (got) {
printf("%d\n", got->value);
got = got->next;
}
printf("\n");
free_nodes(head);
}
close(fd);
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to insert values into a linked list, but keep having the program crash on me. I know I'm doing something wrong I'm just not sure what and after spending 8 hrs trying different methods, I'm ready to ask for help.
I've tried many different ways to get this to work. Sometimes I seem to be able to get the nodes stored and connected, but then when I try to print, it will either only print the first node or print nothing at all.
typedef struct histogram {
char *word;
int count;
struct histogram *next;
} List;
static List *createWord(char word[]) {
char *wordPtr = word;
List *node = (List *)malloc(sizeof(List));
node->word = wordPtr;
node->count = 1;
return node;
}
static void insertAtTail(List **head, List *node) {
List *previous = *head;
if (*head == NULL) {
*head = node;
} else {
while (previous->next != NULL) { // error location
previous = previous->next;
}
previous->next = node;
node->next = NULL;
}
}
void readMain(char *fileName) {
// responsible for read operatons and list storage.
// Counts total words and uniques than stores words in linked list.
char word[100];
char *wordArray[1500] = { NULL };
static int noOfWords = 0;
static int uniqueWords = 0;
List *head = NULL;
List *temp = NULL;
fileRead(inputFile);
while (fscanf(inputFile, "%s", word) == 1) {
if (determineIfWord(word) == 0) {
noOfWords++;
temp = createWord(word);
insertAtTail(&head, temp); // error occurs here
if (!compareWords(wordArray, word, uniqueWords)) {
wordArray[uniqueWords] = calloc(strlen(word) + 1,
sizeof(char));
if (wordArray[uniqueWords] == NULL) {
printf("calloc failed to allocate memory\n");
exit(0);
}
strcpy(wordArray[uniqueWords], word);
uniqueWords++;
}
}
fclose(inputFile);
freeArray(wordArray, uniqueWords);
noOfWords -= 1;
printf("\n%s processed: %i unique words found.\n\n", fileName, uniqueWords);
}
I need the nodes to be stored and linked together allowing me to access the linked list.
You write way too complicated code.
#include <stdbool.h>
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRING(x) #x
#define STRINGIFY(x) STRING(x)
#define BUFFER_SIZE 100
typedef struct node_tag {
char *data;
struct node_tag *next;
} node_t;
node_t* node_create(char const *word)
{
node_t *new_node = calloc(1, sizeof *new_node);
if (!new_node)
return NULL;
new_node->data = malloc(strlen(word) + 1);
if (!new_node->data) {
free(new_node);
return NULL;
}
strcpy(new_node->data, word);
return new_node;
}
void node_free(node_t *node)
{
assert(node && node->data);
free(node->data);
free(node);
}
node_t* node_advance(node_t *node)
{
assert(node);
return node->next;
}
typedef struct list_tag {
node_t *head;
node_t *tail;
size_t length;
} list_t;
list_t list_create(void)
{
list_t list = { NULL, NULL, 0 };
return list;
}
void list_free(list_t *list)
{
assert(list);
for (node_t *current_node = list->head; current_node;) {
node_t *next_node = node_advance(current_node);
node_free(current_node);
current_node = next_node;
}
}
bool list_append(list_t *list, char const *word)
{
assert(list && word);
node_t *new_node = node_create(word);
if (!new_node) {
return false;
}
if (!list->tail) {
list->head = list->tail = new_node;
}
else {
list->tail->next = node_create(word);
list->tail = list->tail->next;
}
++(list->length);
return true;
}
bool list_contains(list_t *list, char const *word)
{
assert(list && word);
for (node_t *current_node = list->head; current_node; current_node = node_advance(current_node)) {
if (strcmp(current_node->data, word) == 0)
return true;
}
return false;
}
void list_print(list_t *list)
{
assert(list);
for (node_t *current_node = list->head; current_node; current_node = node_advance(current_node)) {
puts(current_node->data);
}
}
int main(void)
{
char const *filename = "test.txt";
FILE *input = fopen(filename, "r");
if (!input) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", filename);
return EXIT_FAILURE;
}
size_t unique_words = 0;
list_t words_list = list_create();
for (char buffer[BUFFER_SIZE + 1]; fscanf(input, "%" STRINGIFY(BUFFER_SIZE) "s", buffer) == 1;) {
if (!list_contains(&words_list, buffer))
++unique_words;
if (!list_append(&words_list, buffer)) {
fclose(input);
list_free(&words_list);
fputs("Not enough memory :(\n\n", stderr);
return EXIT_FAILURE;
}
}
puts("Words found:");
list_print(&words_list);
printf("\nNumber of unique words: %zu\n\n", unique_words);
fclose(input);
list_free(&words_list);
}
Here are some problems in the code:
the header files are missing. You need at least <stdio.h>, <stdlib.h> and <string.h>
There is a missing } before the fclose(inputFile);.
The function createWord should allocate a copy of the word, otherwise all nodes point to the same array in main(), where fscanf() writes the last word read from the file.
you should protect against buffer overflow by telling fscanf() the maximum number of characters to store to the destination array: fscanf(inputFile, "%99s", word)
Here is a modified version of createWord:
static List *createWord(const char *word) {
List *node = (List *)malloc(sizeof(List));
if (node) {
node->word = strdup(wordPtr);
node->count = 1;
}
return node;
}
I'm having trouble reading in values to a linked list using fscanf in a loop.
The program ultimately compiles and runs, but the print function is not producing any output which leads me to believe that my linked list is not actually being created in my read function.
I set up a few error checking printf statements and discovered that my student *head value is NULL when it is returned back to main and then sent to the print function.
My functions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student{
char* first;
char* last;
int i;
int j;
struct Student* next;
}student;
int main(int argc, char* argv[])
{
student *start = NULL;
char *studentInput = argv[1];
start = buildStudentList(studentInput,start);
printStudents(start);
free_students(start);
return 0;
}
student* buildList(char* studentsFile, student* head)
{
student *current = head;
char *f = malloc(sizeof(char) * 25);
char *l = malloc(sizeof(char) * 25);
int prior,level = 0;
FILE *in = fopen(studentsFile, "r");
if(in == NULL)
{
printf("\nThe input file failed to open\n");
return NULL;
}
else
{
while(!feof(in))
{
fscanf(in, "%s %s %d %d",f,l,&prior,&level);
student *new = (student*)malloc(sizeof(student));
new->first = (char*)malloc(sizeof(char) * 25);
new->last = (char*)malloc(sizeof(char) * 25);
current = new;
strcpy(current->first,f);
strcpy(current->last,l);
current->i = prior;
current->j = level;
current->next = NULL;
current = current->next;
}
free(first);
free(last);
fclose(in);
return head;
}
}
void print(student* head)
{
if(head == NULL)
{
printf("head is null");
return;
}
student *current = head;
while(current->next != NULL)
{
printf("\n\nFirst Name: %s\nLast Name: %s\nPriority: %d\nReading Level: %d\n\n", current->first, current->last, current->i, current->j);
current = current->next;
printf("makes to loop");
}
}
void free_students(student *head)
{
if(head == NULL)
{
return;
}
student *current = head;
while(current->next != NULL)
{
student *temp = current;
current = current->next;
free(temp->first);
free(temp->last);
free(temp);
}
free(current->first);
free(current->last);
free(current);
}
At this point the output is "head is null".
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.