gpt4 book ai didi

c - 逐行读取文件,它只存储文件中的最后一行

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:41 25 4
gpt4 key购买 nike

我必须从如下所示的 .txt 文件中读取:

New York,4:20,3:03Kansas City,12:03,3:00North Bay,16:00,0:20Kapuskasing,10:00,4:02Thunder Bay,0:32,0:31

I am trying to separate each element into its own array is the end goal so I can use it for something else.

My while loop reads the file correctly, but is only storing the last line of the file in an array and I cant figure out the reason for this. Also file being read could be any number of lines. I am sure its reading each line, as it prints each line it reads perfectly. So I believe the issue lies in storing what it is reading.

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

#pragma warning(disable: 4996)

// a function to remove the trailing carriage return
void clearTrailingCarraigeReturn(char *buffer);

/* == FUNCTION PROTOTYPES == */

/* == CONSTANTS == */
#define RECORD_SIZE 256
#define NUM_RECORDS 5
#define CHUNK_SIZE 1024
#define STRING_SIZE 80

// MAIN
int main(int argc, char *argv[]) {
FILE *fp;
char flightInfo[RECORD_SIZE] = { 0 };
char cityName[20] = {};
char flightHour[20] = {};
char flightMin[20] = {};
char layoverHour[20] = {};
char layoverMin[20] = {};
int i = 0;

struct flightInfo {
char flightName[20];
double flightTime;
double layoverTime;
};

fp = fopen(argv[1], "r");
// first - we'll check the command-line arguments to ensure that the user specified
// a single argument - which we will assume is the name of a file
if (argc != 2) {
printf("Sorry - you need to specify the name of a file on the command line.\n");
return -1;
}

if (fp == NULL) {
printf("Can't open the TEXT file for reading\n");
return -4;
}

// get each of the lines from the file
while (fgets(flightInfo, sizeof flightInfo, fp) > 0) {
clearTrailingCarraigeReturn(flightInfo);
// display the line we got from the file
printf(" >>> read record [%s]\n", flightInfo);
}

// we exited the reading loop - was it because we read the EOF?
if (feof(fp)) {
printf(" [DONE reading the file ... we've reached the EOF]\n");
} else {
// we exited the loop because of an error
if (ferror(fp)) {
// there's an error
printf("Error reading a record from the file\n");
if (fclose(fp) != 0) {
// we can't even close the file
printf("Can't close the TEXT file we opened for reading\n");
}
return -5;
}
}
}

// This function locates any carriage return that exists in a record
// and removes it ...
void clearTrailingCarraigeReturn(char *buffer) {
char *whereCR = strchr(buffer, '\n');
if (whereCR != NULL) {
*whereCR = '\0';
}
}

最佳答案

您的代码中存在多个问题:

  • 您应该在尝试打开文件之前测试命令行参数是否存在。
  • 您的读取循环测试不正确:fgets() 返回指向目标数组的指针或 NULL,因此您不应使用 > 0 而不是:

    while (fgets(flightInfo, sizeof flightInfo, fp) != 0)
  • 字符'\n' 称为换行符,而不是回车符。在遗留平台上,\n 在文本文件 0D 0A 中被转换为 2 个字节,即:回车 和 < em>换行。

  • 循环读取文件内容,但不解析行内容并将其存储到您为此定义的变量中。您应该解析该行,转换值并将信息存储到 flighInfo 结构数组中的下一个条目:

        if (sscanf(flightInfo, "%19[^,],%2[0-9]:%2[0-9],%2[0-9]:%2[0-9]",
    cityName, flightHour, flightMin, layoverHour, layoverMin) == 5) {
    /* convert the times into `double` values and store the info */
    } else {
    /* report the error, exit */
    }
  • struct flightInfo 标记和 char 数组使用相同的名称。这是令人困惑和容易出错的。您应该将 char 数组重命名为 linebuf

  • 在任何情况下您都应该关闭该文件。

关于c - 逐行读取文件,它只存储文件中的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55529038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com