簡體   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