首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态地将行(来自结构)分配到C中的文件中

动态地将行(来自结构)分配到C中的文件中
EN

Stack Overflow用户
提问于 2014-02-18 23:39:53
回答 1查看 78关注 0票数 0

我会尽量迅速和清晰的。我想做的是一个“数据库”,连锁餐厅有很多地方。

该程序以二进制方式编写一个文件"sedi.dat“,该文件在程序中使用100个空记录初始化,该记录指的是结构"ristorante”。

代码语言:javascript
复制
typedef struct {
    int codice;
    char sede[12];
    int posti, liberi, occupati;
} ristorante;

ristorante buffer = { 0, "", 0, 0, 0};
//fopen stuff
for (i=0; i<100; ++i) {
    fwrite(&buffer, sizeof(ristorante), 1, pointer);
};

现在我有100个二进制行,基本上是{ 0, "", 0, 0, 0};。对于这个“格式化”文件,我想做的是用用户输入的行填充它。

我创建了一个函数来执行这个空create_table (文件*指针);它基本上(应该)做的是接收文件指针,创建2个空缓冲区(用于从输入和文件中存储数据,但我可以更改它),然后循环直到指针到达文件的末尾,或者循环内的if给出flag=1 (当从结构中的第一个0开始的行被找到时),否则它会增加计数器并移动偏移计数器*相当大的( struct ),然后加载它用fread写的东西!

现在,问题是这个函数只能加载第一行(通过多次尝试,它只写第一行),它无法查找其他行!我想做的是:

代码语言:javascript
复制
read row
if (first value of struct!=0) {
    ++counter;
    seek next row
}
else {
    write input row with increasing first value starting from 1 to 100
}

我试了两天,又试了又找,试了两天,但现在是寻求帮助的时候了!程序菜单

  1. 打印所有行(我删除了如果该行为空,则不打印该行的if )
  2. 插入新行
  3. 必须进一步检查才能正常工作
  4. 出口

希望我已经说清楚了,现在我在你的手中:)

以下是整个代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#define def 100

typedef struct {
    int codice;
    char sede[12];
    int posti, liberi, occupati;
} ristorante;

typedef struct {
    int number, posti, codice;
} prenotazione;

int scelta_menu (void);
int new_prenotazione (prenotazione max, FILE *pointer, int i);
void stampa_tabella (FILE *pointer);
void create_table (FILE *pointer);


int main () {
    prenotazione max; 
    int scelta, i;
    int conta_entry=0;
    FILE *pointer;
    ristorante buffer = { 0, "", 0, 0, 0};

    while((scelta=scelta_menu())!=5) {
        if((pointer=fopen("sedi.dat", "rb+"))==NULL)  {

            printf("\nTabella sedi assente, ne verra' creata una nuova.\n\n");
            pointer=fopen("sedi.dat", "wb+");

            for (i=0; i<def; ++i) {
                fwrite(&buffer, sizeof(ristorante), 1, pointer);
            }

        }
        else {
            printf("\nFile caricato con successo!");

            switch (scelta) {

            case 1: 
                 stampa_tabella(pointer);
                 break;

            case 2: 
                 create_table(pointer);
             break;

            case 3: 
                 conta_entry=new_prenotazione(max, pointer, conta_entry);
                 break;

         case 4: 
                  printf("La prenotazione con il maggior numero di posti e' la #%d per #%d posti\n\n", max.codice, max.posti);
                  break;

         default:
                  printf("\nScelta non corretta!\n\n");
                  break;

            }

        }

        fclose(pointer);

    }
    return 0;
}



int scelta_menu (void) {
    //system("cls");
    int scelta;
    printf("\n\nMenu Prenotazione:\n\n1 - Visualizza Tabella\n2 - Crea/Aggiorna Tabella\n3 - Nuova prenotazione\n4 - Visualizza prenotazione  piu' grande\n5 - Esci\n\n---> ");
    scanf("%d", &scelta);

    return(scelta);
}

void create_table (FILE *pointer) {

    int i=0;
        int flag=0;
ristorante buffer = { 0, "", 0, 0, 0};

    ristorante buffer1 = { 0, "", 0, 0, 0};

    printf("\nInserisci il nome della sede ed il numero totale dei posti: ");

    scanf("%s%d", buffer.sede, &buffer.posti);
buffer.liberi=buffer.posti;

    printf("\n\n\n%-6s%8s        %s     %s      %s\n", "Cod.", "Sede", "Posti", "Occupati", "Liberi");

    printf("%-6d%10s         %d         %d             %d\n", buffer.codice, buffer.sede, buffer.posti, buffer.occupati, buffer.liberi);

     do {

        fread(&buffer1, sizeof(ristorante), 1, pointer);

        if (buffer1.codice!=0) {

        flag=0;

        ++i;

        fseek(pointer, (i)*sizeof(ristorante), SEEK_SET);

        }

        else {

        flag=1;

        printf("\nEsisto...\n");

        buffer.codice=buffer1.codice+1;

        fwrite(&buffer, sizeof(ristorante), 1, pointer);

        }

    } while(!feof(pointer) && flag!=1);

     fclose(pointer);

     system ("pause");
}



    void stampa_tabella (FILE *pointer) {

    ristorante buffer = { 0, "", 0, 0, 0};

    printf("\n\n\n%4s%8s        %s     %s      %s\n", "Cod.", "Sede", "Posti", "Occupati", "Liberi");

    while(!feof(pointer)) {

     //fseek(pointer, i*sizeof(ristorante), SEEK_SET);

     fread(&buffer, sizeof(ristorante), 1, pointer);

     // if (buffer.codice!=0) {
 printf("%d%10s         %d         %d             %d\n", buffer.codice, 

     buffer.sede, buffer.posti, buffer.occupati, buffer.liberi);

    //}
     }

    fclose(pointer);
    } //fine stampa tabella
    int new_prenotazione (prenotazione max, FILE *pointer, int i) {
prenotazione buffer;
ristorante rist;


    printf("\nInserisci il codice della sede ed il numero dei posti da prenotare: ");
        scanf("%d%d", &buffer.codice, &buffer.posti);
        //sposto puntatore file

    fseek(pointer, (buffer.codice)*sizeof(ristorante), SEEK_SET);

    //legge record

    fread(&rist, sizeof(ristorante), 1, pointer);

    // errore se la sede non esiste

    if(rist.codice!=buffer.codice) {

        printf("\n\nLa sede %d non esiste!\n\n", buffer.codice);

     }

     //altrimenti aggiorna il record del file sede


       else {

      ++i;

        buffer.number=i;

        rist.liberi-=buffer.posti;

        rist.occupati+=buffer.posti;

        printf("\nPrenotazione effettuata! ");

        printf("\nCodice Sede Posti Occupati Liberi\n");

        fwrite(&rist, sizeof(ristorante), 1, pointer);
        }

    if(buffer.posti>=max.posti) {
            max=buffer;


      }


       rewind(pointer);
        return (i);
    } //fine new_record

     //end**
EN

回答 1

Stack Overflow用户

发布于 2014-02-19 01:12:17

听起来你想要的是随机存取文件I/O。这与固定长度的记录相结合,允许读取和写入文件中的单个条目,而不必读取/写入整个文件。您可能需要维护一个显式索引(或多个)来支持这一点。

注意:如果你所说的插入是指在现有条目之间插入,那就太麻烦了。与数组一样,要插入记录,您必须首先将所有其他记录向上移动,以便为其腾出空间,这意味着读取和重写所有记录(并确保在此过程中不要让它们相互践踏)。根据您正在做的事情和原因,更好的答案可能是将新记录附加到文件的末尾,并担心只在索引中排序。

删除可以通过从活动数据索引中删除记录并将其添加到“空记录”列表中进行处理,以便进行可能的重用(而不是附加到文件的末尾)。

基本上,它与内存管理和/或处理固定长度字符串数组的问题集相同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21867932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档