Tai M. Tai M. - 2 months ago 6
C Question

Reading from file, but I'm not able to print the last line?

I'm attempting to read in from a file using fscanf (teacher's pretty much requiring this, I personally would use getline or something else) and I'm attempting to read until the end of the file - my code seems to work fine, except that it doesn't seem to print out the last line of the file I'm reading when I return to the outer loop and I'm not sure why (it does print it when I call the readLine function and print out the lines that I get inside that function however)

If someone could look over my code and let me know where I'm going wrong, I'd really appreciate it. (Please ignore the rather weird looking if-statements in main, that's for future code that I haven't gotten to yet.)

most_freq.h

#ifndef MOST_FREQ_H_
#define MOST_FREQ_H_

#include <stdio.h>

//used to hold each "word" in the list
typedef struct word_node
{
char *word;
unsigned int freq; //frequency of word
struct word_node *next;
} word_node;

struct node *readStringList(FILE *infile);

int readLine(FILE *infile, char * line_buffer);

struct node *getMostFrequent(struct word_node *head, unsigned int num_to_select);

void printStringList(struct word_node *head);

void freeStringList(struct word_node *head);

int InsertAtEnd(char * word, word_node *head);

char *strip_copy(const char *s); //removes any new line characters from strings

#endif


most_freq.c

#include "most_freq.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct word_node *head = NULL; //unchanging head node
char* str_buffer = NULL;

struct node *readStringList(FILE *infile) {
char* temp_buffer = malloc (sizeof(char) * 255); //buffer for 255 chars
while(readLine(infile, temp_buffer) == EXIT_SUCCESS && !feof(infile)) { //while there is still something to be read from the file
printf("Retrieved Line: %s\n", str_buffer);
}
}
int readLine(FILE *infile, char * line_buffer) {
fscanf(infile, "%s", line_buffer);
str_buffer = strdup(line_buffer);
if(str_buffer[0] != '\0' || strcmp(str_buffer, "") != 0) {
return EXIT_SUCCESS; //return success code
}
else {
return EXIT_FAILURE; //return failure code
}
}

int InsertAtEnd(char * word, word_node *head){
}

void printStringList(struct word_node *top) {
}

char *strip_copy(const char *s) {
}

int main(int argc, char *argv[])
{
if (argc == 2) // no arguments were passed
{
FILE *file = fopen(argv[1], "r"); /* "r" = open for reading, the first command is stored in argv[1] */
if ( file == 0 )
{
printf( "Could not open file.\n" );
}
else
{
readStringList(file);
}
}
else if (argc < 3) {
printf("You didn't pass the proper arguments! The necessary arguments are: <number of most frequent words to print> <file to read>\n");
}
}


text file

foofoo
dog
cat
dog
moom
csci401isfun
moon$
foofoo
moom.
dog
moom
doggod
dog3
f34rm3
foofoo
cat

Answer

After the last line is read, the File is at the end. Your function still returns EXIT_SUCCESS (and the last line), but you additionally check for EOF: ... && !feof(infile), so processing ends before the last line is printed.