I'm a beginner in C and I'm trying to create a simple todo list program. I'm trying to use getline in a while loop, as I saw on another stack overflow answer and I thought I understood it but it's just creating an infinite loop. Also it seems to be skipping the first word for some reason. Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *list;
int i = 0;
int item = 0;
char items[10];
char chars[1000];
char * line = NULL;
size_t len = 0;
ssize_t read;
int main() {
list = fopen("/Users/bendavies/Documents/C/list.txt", "r");
int letterCount = fscanf(list,"%s",chars);
printf("Welcome to the to-do list. It can hold up to 10 items.\n");
printf("%d\n", letterCount);
if (letterCount == -1) {
printf("The list currently has no items!\n");
} else {
while ((read = getline(&line, &len, list)) != 1) {
item += 1;
printf("%d. %s", item, line);
}
}
fclose(list);
return 0;
}
The output I'm currently getting with the following list.txt:
Eat food
Drink water
Breath air
Is:
1. food
2. Drink water
3. Breath air
4. 18446744073709551615
5. 18446744073709551615
and so on and so forth.
Thank you in advance! :)