- #1
diredragon
- 323
- 15
Homework Statement
My task is to write a program that reads from the .txt file into a list which contains two parts, the part of the text from the file and a pointer to the next element. Here are the specifications of the problem:
When reading from the file you should separate the letters from the numbers in your list (Space is a default separator). For example:
If your .txt file contains the following: I am 39 years old and my username is 7yghh67f[]\.
Your code should store: (comas here represent the next list element)
I, am, 39, years, old, and, my, username, is, 7, yghh, 67, f, []\
2. Homework Equations
3. The Attempt at a Solution
I hope you understand what i need help with. So far i have written an algorithm to separate the .txt file text into blocks of words not including spaces.
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct words {
char *text;
struct words *nextp;
} Words;
Words* read(FILE *document, Words *p) {
int i = 0, j;
char c;
Words *new = p;
new = calloc(1, sizeof(Words));
while ((c = fgetc(document)) != EOF) {
if (c == ' ') {
new->text[i] = '\0';
i = 0;
new = calloc(1, sizeof(Words));
new = new->nextp;
}
else {
new->text[i] = c;
new->nextp = NULL;
i++;
}
}
return p;
}
void print(Words *p) {
while (p) {
puts(p->text);
p = p->nextp;
}
}
int main(int barg, const char *varg[]) {
FILE *document = NULL;
Words *headt = NULL;
document = fopen(varg[1], "r");
headt = read(document, headt);
print(headt);
fclose(document);
}