The code does not work because you allocate space for lines of 30 characters plus a newline and null terminator, and because you only print one line after feof() returns true.
In addition, feof() returns true only after you tried and could not read the end of the file. This means that while (!feof(fp)) is usually incorrect - you just have to read until the read function works - at this point you can use feof() / ferror() to distinguish between end errors file and other types (if you need to). So the code might look like this:
char line[34]; while (fgets(line, 34, fp) != NULL) { printf("%s", line); }
If you want to find the first character '\n' in line and replace it with '\0' , you can use strchr() from <string.h> :
char *p; p = strchr(line, '\n'); if (p != NULL) *p = '\0';
source share