Text file word by word into Link List

I'm having an issue within the first while loop, it prints out 1 then crashes. Eclipse doesn't give me any errors. It pops up a windows alert box saying Assignment2 has stopped working. I think it has something to do with memory location access or something. Any idea why this won't work properly? It's suppose to read in a text file and store it word by word.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

struct listNode { /* self-referential structure */
	char* data;
	struct listNode *nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

int main() {
	LISTNODEPTR startPtr = malloc(sizeof(LISTNODE));
	LISTNODEPTR current = startPtr;
	int choice, num;

	char item;
	char file_name[20];
	FILE *fptr;
	long numBytes;
	int file;
	int read = 1;
	char *token;
	setvbuf(stdout, NULL, _IONBF, 0);
	printf("Type in the name of the file containing the Field\n");
	scanf("%s", &file_name);

	fptr = fopen(file_name, "rt");

//having an issue with this while loop
	while (read != 0) {
		printf("%d", read);
		current->nextPtr = malloc(sizeof(LISTNODE));
		current = current->nextPtr;
		current->data = NULL;
		read = fscanf(fptr, "%s", current->data);
		printf("\n");
	}

	fclose(fptr);

	printf("Type in the number of characters per line:\n");
	scanf("%d", &num);
	return 0;
}

	}
closed account (D80DSL3A)
It's probably crashing at line 36 where it tries to write a string to a NULL pointer.
There is no memory allocated to data. Is this what lines 42, 43 are for?
If you put that after line 6 then you would know how many characters to allocate to data so the string can be stored there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
printf("Type in the name of the file containing the Field\n");
	scanf("%s", &file_name);
printf("Type in the number of characters per line:\n");
	scanf("%d", &num);

	fptr = fopen(file_name, "rt");

//having an issue with this while loop
	while (read != 0) {
		printf("%d", read);
		current->nextPtr = malloc(sizeof(LISTNODE));
		current = current->nextPtr;
//		current->data = NULL;
                current->data = (char*)malloc(num*sizeof(char));//allocate storage for the string
		read = fscanf(fptr, "%s", current->data);
		printf("\n");
                current->nextPtr = NULL;// mark as end of list, in case this is the last node.
	}
Last edited on
Sorry i didn't explain the overall goal of the code. The user will be asked to enter in the number of characters per line so i can print the file with type justification depending on the number of characters they want per line.

It reads a file of text so how am i suppose to determine the size of current->data?
closed account (D80DSL3A)
Can you anticipate a maximum length for the strings being read from the file?
I don't program in c so I don't know the methods well;

I would declare a char array that's really big.
char temp[1000];
then read each string into that
read = fscanf(fptr, "%s", temp);
then find the actual string length
int length = strlen(temp) + 1;// allow room for the terminating '\0'
then allocate that much to data
current->data = (char*)malloc(length*sizeof(char));
finally, copy from temp to data
strcpy(current->data, temp);
Last edited on
i tweaked my code a little bit and it prints semi-justified. The only issue i have now is trying to add spaces in between each word to even it out. So if i specify there can only be 20 characters printed on each line, and all the words only add up to say, 16 characters, i need to distribute 4 spaces inbetween the words. How do i go about doing this? I should have some sort of loop inside my loop that prints the current->data?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>

struct listNode { /* self-referential structure */
	char data[50];
	struct listNode *nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

int main() {
	LISTNODEPTR startPtr = malloc(sizeof(LISTNODE));
	LISTNODEPTR current = startPtr;
	int choice, num = 0;

	char item;
	char file_name[20];
	FILE *fptr;
	int file;
	int read = 1;
	int length = 0;
	setvbuf(stdout, NULL, _IONBF, 0);
	printf("Type in the name of the file containing the Field\n");
	scanf("%s", &file_name);

	fptr = fopen(file_name, "rt");

	while (!feof(fptr)) {
		current->nextPtr = malloc(sizeof(LISTNODE));
		current = current->nextPtr;
		fscanf(fptr, "%s", current->data);
	}
	current = startPtr->nextPtr;
	fclose(fptr);

	printf("Type in the number of characters per line:\n");
	scanf("%d", &num);

	if (current == NULL )
		printf("List is empty.\n\n");
	else {
		printf("The list is:\n");

		while (current != NULL ) {
			if ((length + strlen(current->data)) < num) {
				printf("%s ", current->data);
				//printf("\ncurrent length: %d", length);
				length += strlen(current->data) + 1;
				//printf("\nnew length: %d", length);
				current = current->nextPtr;
			} else {
				length = 0;
				printf("\n");
			}
		}
		return 0;
	}
}
Topic archived. No new replies allowed.