c - 从文件中读取,但我无法打印最后一行?

标签 c file-handling

我正在尝试使用 fscanf 从文件中读入(老师几乎要求这样做,我个人会使用 getline 或其他东西)并且我正在尝试读取直到文件末尾 - 我的代码似乎可以工作很好,除了当我返回到外循环时,它似乎没有打印出我正在读取的文件的最后一行,我不确定为什么(当我调用 readLine 函数并打印出它时,它确实打印了它)然而我在那个函数中得到的行)

如果有人可以查看我的代码并让我知道哪里出了问题,我将非常感激。 (请忽略 main 中看起来相当奇怪的 if 语句,那是我还没有接触到的 future 代码。)

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");
    }
}

文本文件

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

最佳答案

读取最后一行后,文件位于末尾。您的函数仍然返回 EXIT_SUCCESS (和最后一行),但您还检查 EOF:... && !feof(infile),因此处理在打印最后一行之前结束。

关于c - 从文件中读取,但我无法打印最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431580/

相关文章:

c - 永无止境的快速排序

java - 无论输入什么,都只会执行 if...else 梯形图的 else 语句。为什么会发生这种情况?

c# - 就地修改 XML 文件?

c - 检测不完整的 32 位二进制数据

c - 什么时候使用 char a[] 而不是 char p* ,反之亦然?

c - 使用定时器中断使 LED 闪烁(atmega 128)

android - AIDE 仅支持原生 Android 应用程序?

c - 如何从 x86_64 windows 异常处理程序中正确调用 rtlunwind

java - java中的zip创建错误

c - 关于使用 c 写入文件的查询