簡體   English   中英

重新閱讀C中的結構

[英]Re-reading structure in C

我試圖從文件中讀取結構。 一切都是正確的,直到我關閉我的應用程序並再次打開它嘗試讀取數據。

  1. 寫結構到文件(正確)
  2. 從文件中讀取結構(正確)
  3. 關閉應用程序
  4. 從文件中讀取結構(不正確)
  5. 項目清單

處理終止,狀態為-1073741819(0分鍾,6秒(s))

我的代碼:logika.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "logika.h"
int write_struc(char *PF, char *DF, char *TF, char *TL)
{
    struct MYSTR myStruct ={PF, DF, TF, TL};
    FILE* fb;
    if ((fb=fopen(PLIK_DAT, "a"))!=NULL)
    {
        fwrite(&myStruct, sizeof(struct MYSTR), 1, fb);
        fclose(fb);
        return 1;
    }
    else
    {
        return 0;
    }
}

void read_struc()
{
    FILE* fb;
    struct MYSTR myStruct;
    fb = fopen(PLIK_DAT, "r");
    while(1) {
        fread(&myStruct,sizeof(struct MYSTR),1,fb);
        if(feof(fb)!=0)
            break;
        printf("%s",myStruct.PF);
        printf("%10s" ,myStruct.DF);
        printf("%10s" ,myStruct.TF);
        printf("%10s\n", myStruct.TL);
    }
    fclose(fb);
}

logic.h

#ifndef LOGIKA_H_INCLUDED
#define LOGIKA_H_INCLUDED
#define PLIK_DAT "baza.dat"
int write_struc(char *PF, char *DF, char *TF, char *TL);
struct MYSTR{
    char *PF;
    char *DF;
    char *TF;
    char *TL;
};
#endif // LOGIKA_H_INCLUDED

我已經嘗試過二進制寫入/讀取並且沒有幫助。

您的結構包含4個地址。 這些地址在您的程序之外毫無意義。 如果你期望字符串數據(結構中包含的地址是什么)被寫出來,你必須自己做,並設計一個文件格式,允許你重建輸入數據。

在你的程序的一次執行中,你得到“幸運”,因為這些地址的數據沒有改變,所以它看起來像是有效的。 但它沒有任何用處。

以下提議的代碼:

  1. 干凈利落地編譯
  2. 正確檢查錯誤
  3. 正確定義用於讀取/寫入磁盤文件的結構
  4. 修改了定義函數原型的位置

注意:我將頭文件合並到主代碼中,您可能不想這樣做。

現在,建議的代碼:

#ifndef LOGIKA_H_INCLUDED
#define LOGIKA_H_INCLUDED

// prototypes
int write_struc(char *PF, char *DF, char *TF, char *TL);
void read_struc( void );

#define MAX_STRING_LEN 1024

struct MYSTR
{
    char PF[ MAX_STRING_LEN ];
    char DF[ MAX_STRING_LEN ];
    char TF[ MAX_STRING_LEN ];
    char TL[ MAX_STRING_LEN ];
};
#endif // LOGIKA_H_INCLUDED


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include "logika.h"

#define MAX_BUF_LEN 256
#define PLIK_DAT "baza.dat"

int write_struc(char *PF, char *DF, char *TF, char *TL)
{
    struct MYSTR myStruct;
    strncpy( myStruct.PF, PF, MAX_STRING_LEN );
    strncpy( myStruct.DF, DF, MAX_STRING_LEN );
    strncpy( myStruct.TF, TF, MAX_STRING_LEN );
    strncpy( myStruct.TL, TL, MAX_STRING_LEN );

    FILE* fb;
    if ( !(fb=fopen(PLIK_DAT, "a")))
    {
        char buffer[ MAX_BUF_LEN ];
        sprintf( buffer, "fopen to append to %s failed", PLIK_DAT );
        perror( buffer );
        return 0;
    }

    // implied else, fopen successful

    if(  fwrite(&myStruct, sizeof(struct MYSTR), 1, fb) != 1)
    {
        perror( "fwrite failed" );
        return 0;
    }

    // implied else, fwrite successful

    fclose(fb);
    return 1;
}


void read_struc()
{
    struct MYSTR myStruct;
    FILE* fb;
    if( !(fb = fopen(PLIK_DAT, "r") ) )
    {
        char buffer[ MAX_BUF_LEN ];
        sprintf( buffer, "fopen to read %s failed", PLIK_DAT );
        perror( buffer );
        exit( EXIT_FAILURE );  // if program can recover, then use `return;`  rather than `exit()`
    }

    // implied else, fopen successful

    while( fread(&myStruct,sizeof(struct MYSTR),1,fb) == 1)
    {
        printf("\n%s", myStruct.PF);
        printf("%s",   myStruct.DF);
        printf("%s",   myStruct.TF);
        printf("%s\n", myStruct.TL);
    }
    fclose(fb);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM