繁体   English   中英

fgets()无法从文本文件读取?

[英]fgets() not reading from a text file?

我有一个函数loadets()( 加载设置的缩写),应该从名为Progsets.txt的文本文件加载设置。 loadets()成功返回0,检测到致命错误时返回-1。 但是, 实际上Progsets.txt中读取的代码部分(三个fgets() )似乎都失败了并返回了空指针,因此除了一堆空值外根本不加载任何东西。 我的代码有问题吗? 当我运行代码时,fp是有效的指针,并且能够打开它进行读取。 那怎么了

此代码用于使用cmd加载我非常基本的文本编辑器程序的默认文本颜色。

标头:

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

#define ARR_SIZE 100

struct FINSETS
{
    char color[ARR_SIZE + 1];
    char title[ARR_SIZE + 1];
    char maxchars[ARR_SIZE + 1];
} SETTINGS;

loadset():

int loadsets(int* pMAXCHARS) // load settings from a text file
{
    FILE *fp = fopen("C:\\Typify\\Settings (do not modify)\\Progsets.txt", "r");
    char *color = (char*) malloc(sizeof(char*) * ARR_SIZE);
    char *title = (char*) malloc(sizeof(char*) * ARR_SIZE);
    char *maxchars = (char*) malloc(sizeof(char*) * ARR_SIZE);
    char com1[ARR_SIZE + 1] = "color ";
    char com2[ARR_SIZE + 1] = "title ";
    int i = 0;
    int j = 0;
    int k = 0;
    int found = 0;

    while (k < ARR_SIZE + 1) // fill strings with '\0'
    {
        color[k] = title[k] = maxchars[k] = '\0';
        SETTINGS.color[k] = SETTINGS.maxchars[k] = SETTINGS.title[k] = '\0';
        k++;
    }

    if (!fp) // check for reading errors
    {
        fprintf(stderr, "Error: Unable to load settings. Make sure that Progsets.txt exists and has not been modified.\a\n\n");
        return -1; // fatal error
    }

    if (!size(fp)) // see if Progsets.txt is not a zero-byte file (it shouldn't be)
    {
        fprintf(stderr, "Error: Progsets.txt has been modified. Please copy the contents of Defsets.txt to Progsets.txt to manually reset to default settings.\a\n\n");

        free(color);
        free(title);
        free(maxchars);

        return -1; // fatal error
    }

    // PROBLEMATIC CODE:

    fgets(color, ARR_SIZE, fp);      // RETURNS NULL (INSTEAD OF READING FROM THE FILE)
    fgets(title, ARR_SIZE, fp);      // RETURNS NULL (INSTEAD OF READING FROM THE FILE)
    fgets(maxchars, ARR_SIZE, fp);   // RETURNS NULL (INSTEAD OF READING FROM THE FILE)

    // END OF PROBLEMATIC CODE:

    system(strcat(com1, SETTINGS.color)); // set color of cmd
    system(strcat(com2, SETTINGS.title)); // set title of cmd
    *pMAXCHARS = atoi(SETTINGS.maxchars);

    // cleanup

    fclose(fp);
    free(color);
    free(title);
    free(maxchars);

    return 0; // success
}

Progsets.txt:

COLOR=$0a;
TITLE=$Typify!;
MAXCHARS=$10000;

编辑:这是size()函数的定义。 由于我只使用ASCII文本文件,因此我假设每个字符都是一个字节,并且可以通过计算字符数来得出文件大小(以字节为单位)。 有什么可疑的吗?

尺寸():

int size(FILE* fp)
{
    int size = 0;
    int c;

    while ((c = fgetc(fp)) != EOF)
    {
        size++;
    }

    return size;
}

问题出在您对size()函数的使用上。 它重复调用文件句柄上的fgetc()直到到达文件末尾,递增一个值以跟踪文件中的字节数。

这不是一个方法(尽管我敢肯定,有更好的方法不涉及基于字符的低效率的I / O),但是它确实存在一个致命的缺陷,您似乎已经忽略了。

调用它之后,您将一直读取文件,直到进一步读取为止,例如:

fgets(color, ARR_SIZE, fp);

只会失败,因为您已经在文件末尾了。 size()返回之前,您可能需要考虑rewind()类的东西-它将文件指针放回到文件的开头,以便您可以再次读取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM