C fwrite和fread结构
我想将这个结构写入一个二进制文件,然后从二进制文件中读回,但它不起作用。结构C fwrite和fread结构,c,struct,fwrite,fread,C,Struct,Fwrite,Fread,我想将这个结构写入一个二进制文件,然后从二进制文件中读回,但它不起作用。结构tLista有一个impersora数组,而impersora有一个tCola结构。l->oc的值为256883222,显然不正确。此值取决于loadData函数 typedef char tcadena[13]; typedef struct { tcadena nombres [TAM]; int lectura; int escritura; }tCola; typedef s
tLista
有一个impersora
数组,而impersora
有一个tCola
结构。l->oc
的值为256883222
,显然不正确。此值取决于loadData
函数
typedef char tcadena[13];
typedef struct
{
tcadena nombres [TAM];
int lectura;
int escritura;
}tCola;
typedef struct
{
char nombre [21];
int pendientes;
tCola cola;
}impresora;
typedef struct
{
impresora lista[TAMI];
int oc;
}tLista;
...
void finalizar(tLista *l)
{
/* Apertura del fichero de destino, para escritura en binario*/
FILE *fichero;
fichero = fopen ("listaImpresoras.bin", "wb");
if (fichero==NULL)
{
printf("No se puede abrir listaImpresoras.bin");
}
else
{
fwrite(l, sizeof(*l), 1, fichero);
}
fclose(fichero);
}
...
void inicializarLista (tLista *l)
{
FILE *fichero;
fichero=fopen("listaImpresoras.bin", "r");
if(fichero==NULL)
{
printf("No existe el fichero listaImpresoras.bin\n");
l->oc=0;
}
else
{
fread(l, sizeof(*l), 1, fichero);
}
fclose(fichero);
}
...
int main(int argc, char* argv[])
{
tLista l;
inicializarLista(&l);
loadData(&l); //printf and scanf (user) charge the list
finalizar(&l);
return 0;
}
您有几个问题,但主要问题是您没有初始化
tlistl代码>结构正确,下面我发布一些代码,可能会帮助您看到问题,它可以工作,我测试了它
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TAM 100
#define TAMI 100
typedef char tcadena[13];
typedef struct
{
tcadena nombres[TAM];
int lectura;
int escritura;
}tCola;
typedef struct
{
char nombre[21];
int pendientes;
tCola cola;
}impresora;
typedef struct
{
impresora lista[TAMI];
int oc;
}tLista;
void finalizar(tLista *l)
{
/* Apertura del fichero de destino, para escritura en binario*/
FILE *fichero;
fichero = fopen ("listaImpresoras.bin", "w");
if (fichero == NULL)
{
printf("No se puede abrir listaImpresoras.bin");
}
else
{
fwrite(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void inicializarLista(tLista *l)
{
FILE *fichero;
fichero = fopen("listaImpresoras.bin", "r");
if (fichero == NULL)
{
printf("No existe el fichero listaImpresoras.bin\n");
l->oc = 0;
}
else
{
fread(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void loadData(tLista *l)
{
int i;
for (i = 0 ; i < 10 ; i++)
{
l->oc++;
}
}
int main(int argc, char* argv[])
{
tLista l;
/* tienes que iniacializar todos los campos de `l`. */
memset(&l, 0, sizeof(l));
/* si no quieres hacerlo asi, entonces puede que
*
* l.oc = 0;
*
* sea suficiente, pero quedan los otros campos.
*
* al no inicializar explicitamente los campos, contienen
* basura aleatoria, que has estado escribiendo en el archivo.
*/
inicializarLista(&l);
printf("%d\n", l.oc);
loadData(&l); //printf and scanf (user) charge the list
finalizar(&l);
return 0;
}
#包括
#包括
#包括
#定义TAM 100
#定义塔米100
typedef char tcadena[13];
类型定义结构
{
tcadena nombres[TAM];
国际讲师协会;
内埃斯克里图拉;
}tCola;
类型定义结构
{
char-nombre[21];
内在依赖性;
可乐;
}预付款;
类型定义结构
{
塔米公司董事长;
国际奥委会;
}特利斯塔;
无效终结器(tLista*l)
{
/*德斯蒂诺菲舍罗酒店*/
档案*fichero;
fichero=fopen(“listaImpresoras.bin”,“w”);
if(fichero==NULL)
{
printf(“无需提交任何清单”);
}
其他的
{
fwrite(l,sizeof(*l),1,fichero);
/*solo debes llamar fclose(菲舍罗);si(菲舍罗!=NULL)*/
fclose(fichero);
}
/*fclose(fichero);esto va ser ejecutado,aunque(fichero==NULL)*/
}
无效inicializarLista(tLista*l)
{
档案*fichero;
fichero=fopen(“listaImpresoras.bin”、“r”);
if(fichero==NULL)
{
printf(“不存在el fichero listaImpresoras.bin”);
l->oc=0;
}
其他的
{
fread(1,sizeof(*1),1,fichero);
/*solo debes llamar fclose(菲舍罗);si(菲舍罗!=NULL)*/
fclose(fichero);
}
/*fclose(fichero);esto va ser ejecutado,aunque(fichero==NULL)*/
}
无效载荷数据(tLista*l)
{
int i;
对于(i=0;i<10;i++)
{
l->oc++;
}
}
int main(int argc,char*argv[])
{
特利斯塔;
/*这是一个非常有趣的故事*/
memset(&l,0,sizeof(l));
/*我不想安静下来,周围的人都很安静
*
*l.oc=0;
*
*海洋苏菲森特,佩罗·奎丹·洛斯·奥特罗斯·坎波斯。
*
*康蒂安省洛斯坎波斯市没有明显的歧视
*巴苏拉·阿莱托利亚(basura aleatoria)有一个建筑风格的描述。
*/
inicializarLista(&l);
printf(“%d\n”,l.oc);
loadData(&l);//printf和scanf(用户)对列表收费
最后定稿(&l);
返回0;
}
当您没有显式初始化tLista l中的所有字段时代码>它们包含垃圾,您已将其写入文件
另外,如果您检查了fopen()
的成功,为什么要尝试fclose()
结果NULL
?在fopen()
返回NULL
的情况下,由于fopen
的结果为NULL,您必须退出函数,而不必关闭fopen
的结果,因为它是NULL您有几个问题,但主要的问题是您没有初始化t列表l代码>结构正确,下面我发布一些代码,可能会帮助您看到问题,它可以工作,我测试了它
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TAM 100
#define TAMI 100
typedef char tcadena[13];
typedef struct
{
tcadena nombres[TAM];
int lectura;
int escritura;
}tCola;
typedef struct
{
char nombre[21];
int pendientes;
tCola cola;
}impresora;
typedef struct
{
impresora lista[TAMI];
int oc;
}tLista;
void finalizar(tLista *l)
{
/* Apertura del fichero de destino, para escritura en binario*/
FILE *fichero;
fichero = fopen ("listaImpresoras.bin", "w");
if (fichero == NULL)
{
printf("No se puede abrir listaImpresoras.bin");
}
else
{
fwrite(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void inicializarLista(tLista *l)
{
FILE *fichero;
fichero = fopen("listaImpresoras.bin", "r");
if (fichero == NULL)
{
printf("No existe el fichero listaImpresoras.bin\n");
l->oc = 0;
}
else
{
fread(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void loadData(tLista *l)
{
int i;
for (i = 0 ; i < 10 ; i++)
{
l->oc++;
}
}
int main(int argc, char* argv[])
{
tLista l;
/* tienes que iniacializar todos los campos de `l`. */
memset(&l, 0, sizeof(l));
/* si no quieres hacerlo asi, entonces puede que
*
* l.oc = 0;
*
* sea suficiente, pero quedan los otros campos.
*
* al no inicializar explicitamente los campos, contienen
* basura aleatoria, que has estado escribiendo en el archivo.
*/
inicializarLista(&l);
printf("%d\n", l.oc);
loadData(&l); //printf and scanf (user) charge the list
finalizar(&l);
return 0;
}
#包括
#包括
#包括
#定义TAM 100
#定义塔米100
typedef char tcadena[13];
类型定义结构
{
tcadena nombres[TAM];
国际讲师协会;
内埃斯克里图拉;
}tCola;
类型定义结构
{
char-nombre[21];
内在依赖性;
可乐;
}预付款;
类型定义结构
{
塔米公司董事长;
国际奥委会;
}特利斯塔;
无效终结器(tLista*l)
{
/*德斯蒂诺菲舍罗酒店*/
档案*fichero;
fichero=fopen(“listaImpresoras.bin”,“w”);
if(fichero==NULL)
{
printf(“无需提交任何清单”);
}
其他的
{
fwrite(l,sizeof(*l),1,fichero);
/*solo debes llamar fclose(菲舍罗);si(菲舍罗!=NULL)*/
fclose(fichero);
}
/*fclose(fichero);esto va ser ejecutado,aunque(fichero==NULL)*/
}
无效inicializarLista(tLista*l)
{
档案*fichero;
fichero=fopen(“listaImpresoras.bin”、“r”);
if(fichero==NULL)
{
printf(“不存在el fichero listaImpresoras.bin”);
l->oc=0;
}
其他的
{
fread(1,sizeof(*1),1,fichero);
/*solo debes llamar fclose(菲舍罗);si(菲舍罗!=NULL)*/
fclose(fichero);
}
/*fclose(fichero);esto va ser ejecutado,aunque(fichero==NULL)*/
}
无效载荷数据(tLista*l)
{
int i;
对于(i=0;i<10;i++)
{
l->oc++;
}
}
int main(int argc,char*argv[])
{
特利斯塔;
/*这是一个非常有趣的故事*/
memset(&l,0,sizeof(l));
/*我不想安静下来,周围的人都很安静
*
*l.oc=0;
*
*海洋苏菲森特,佩罗·奎丹·洛斯·奥特罗斯·坎波斯。
*
*康蒂安省洛斯坎波斯市没有明显的歧视
*巴苏拉·阿莱托利亚(basura aleatoria)有一个建筑风格的描述。
*/
inicializarLista(&l);
printf(“%d\n”,l.oc);
loadData(&l);//printf和scanf(用户)对列表收费
最后定稿(&l);
返回0;
}
当您没有显式初始化tLista l中的所有字段时代码>它们包含垃圾,您已将其写入文件
另外,如果您检查了fopen()
的成功,为什么要尝试fclose()
结果NULL
?在fopen()
返回NULL
的情况下,由于fopen
的结果为NULL,您必须退出函数,而不必关闭fopen
的结果,因为它是NULL您有几个问题,但主要的问题是您没有初始化t列表l代码>结构正确,下面我发布一些代码,可能会帮助您看到问题,它可以工作,我测试了它
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TAM 100
#define TAMI 100
typedef char tcadena[13];
typedef struct
{
tcadena nombres[TAM];
int lectura;
int escritura;
}tCola;
typedef struct
{
char nombre[21];
int pendientes;
tCola cola;
}impresora;
typedef struct
{
impresora lista[TAMI];
int oc;
}tLista;
void finalizar(tLista *l)
{
/* Apertura del fichero de destino, para escritura en binario*/
FILE *fichero;
fichero = fopen ("listaImpresoras.bin", "w");
if (fichero == NULL)
{
printf("No se puede abrir listaImpresoras.bin");
}
else
{
fwrite(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void inicializarLista(tLista *l)
{
FILE *fichero;
fichero = fopen("listaImpresoras.bin", "r");
if (fichero == NULL)
{
printf("No existe el fichero listaImpresoras.bin\n");
l->oc = 0;
}
else
{
fread(l, sizeof(*l), 1, fichero);
/* solo debes llamar fclose(fichero); si (fichero != NULL) */
fclose(fichero);
}
/* fclose(fichero); esto va a ser ejecutado, aunque (fichero == NULL) */
}
void loadData(tLista *l)
{
int i;
for (i = 0 ; i < 10 ; i++)
{
l->oc++;
}
}
int main(int argc, char* argv[])
{
tLista l;
/* tienes que iniacializar todos los campos de `l`. */
memset(&l, 0, sizeof(l));
/* si no quieres hacerlo asi, entonces puede que
*
* l.oc = 0;
*
* sea suficiente, pero quedan los otros campos.
*
* al no inicializar explicitamente los campos, contienen
* basura aleatoria, que has estado escribiendo en el archivo.
*/
inicializarLista(&l);
printf("%d\n", l.oc);
loadData(&l); //printf and scanf (user) charge the list
finalizar(&l);
return 0;
}
#包括
#包括
#包括
#定义TAM 1