'Get words and their positions from a file in C
I'm trying to implement a function in C that reads a file and save all the words inside. Moreover, I would like to get the line number, the order in the line and the sentence number.
I tried this :
char word[MAX_LENGTH];
while (fscanf(file, "%s", word) != EOF) {
...
}
And it allows me to get the sentence number (by getting the dots at the end of the words) but not the line number...
Is it possible to get the '\n' characters with this or is there another solution to my problem ?
EDIT : the file contains only letters, whitespaces (word separator), newlines (line separator) and dots (sentence separator).
Solution 1:[1]
The approach that I would try would be a
char *ptr[]
where each pointer in the array would contain a line. This way it would be easy to index each line. I would then keep track of the last character input into each char *, and if its equal to '\n', then increase the index and read into the next char * in the array.
ptr[I+1]
This way each line will be stored in the corresponding char *.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | neutron02 |
