C不';t从文件加载信息(fread、fwrite) #包括 #包括 #包括 #定义SLENG 50//只是一个随机值 typedef结构歌 { 字符*名称; 查*纳松; char*timeSong; 国际日期; }歌曲; void saveToFile(Song*x,int*songCount)//将信息保存到二进制文件中 { 文件*f=fopen(“array.txt”,“w”); 如果(f==NULL) { printf(“错误\n”); } fwrite(songCount,sizeof(int),1,f); fwrite(x,sizeof(struct Song),(*songCount),f); fclose(f); } void readSong(Song*x,int*songCount)//从文件中读取信息并写入 { FILE*fr=fopen(“array.txt”、“r”); 如果(fr==NULL) { printf(“错误\n”); } printf(“歌曲:\n”); fread(songCount,sizeof(int),1,fr); fread(x,sizeof(struct Song),(*songCount),fr); 对于(int i=0;i
为了将结构保存到文件中,它只能包含标量值,而不能包含指向内存对象的指针。请修改结构以使用数组而不是指针:C不';t从文件加载信息(fread、fwrite) #包括 #包括 #包括 #定义SLENG 50//只是一个随机值 typedef结构歌 { 字符*名称; 查*纳松; char*timeSong; 国际日期; }歌曲; void saveToFile(Song*x,int*songCount)//将信息保存到二进制文件中 { 文件*f=fopen(“array.txt”,“w”); 如果(f==NULL) { printf(“错误\n”); } fwrite(songCount,sizeof(int),1,f); fwrite(x,sizeof(struct Song),(*songCount),f); fclose(f); } void readSong(Song*x,int*songCount)//从文件中读取信息并写入 { FILE*fr=fopen(“array.txt”、“r”); 如果(fr==NULL) { printf(“错误\n”); } printf(“歌曲:\n”); fread(songCount,sizeof(int),1,fr); fread(x,sizeof(struct Song),(*songCount),fr); 对于(int i=0;i,c,arrays,struct,fwrite,fread,C,Arrays,Struct,Fwrite,Fread,为了将结构保存到文件中,它只能包含标量值,而不能包含指向内存对象的指针。请修改结构以使用数组而不是指针: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SLENG 50 //just a random value typedef struct Song { char *name; char *nameSong; char *timeSong;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 //just a random value
typedef struct Song
{
char *name;
char *nameSong;
char *timeSong;
int date;
} Song;
void saveToFile(Song *x, int *songCount) //Saves info to the binary file
{
FILE *f = fopen("array.txt", "w");
if (f == NULL)
{
printf("Error\n");
}
fwrite(songCount, sizeof(int), 1, f);
fwrite(x, sizeof(struct Song), (*songCount), f);
fclose(f);
}
void readSong(Song *x, int *songCount) //Reads info fromt he file and writes it
{
FILE *fr = fopen("array.txt", "r");
if (fr == NULL)
{
printf("Error\n");
}
printf("Songs:\n");
fread(songCount, sizeof(int), 1, fr);
fread(x, sizeof(struct Song), (*songCount), fr);
for(int i=0; i < (*songCount); i++)
{
printf("%d. %s %s %s %d\n", (i+1), x[i].name, x[i].nameSong, x[i].timeSong, x[i].date);
}
fclose(fr);
}
void insertSong(Song *x, int Count) //Inserts new song into the array.
{
printf("\nInsert name of the band:\n");
x[Count].name=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].name);
printf("Insert name of the song:\n");
x[Count].nameSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].nameSong);
printf("Insert length of the song:\n");
x[Count].timeSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
main()
{
int songCount, menuOption;
Song *x=malloc(SLENG*sizeof(char)+SLENG*sizeof(char)+SLENG*sizeof(char)+sizeof(int));
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch(menuOption)
{
case(1) :
printf("Insert how many songs do you want to input?\n");
scanf("%d", &songCount);
for(int i=0; i<songCount; i++)
{
insertSong(x, i);
}
saveToFile(x, &songCount);
break;
case(2) :
readSong(x, &songCount);
break;
}
}
并相应地修改代码,但请注意:
- 在文件中保存和读取结构需要以二进制模式打开它
- 给二进制文件命名
是非常容易误导的array.txt
- 写入文件时不需要传递计数的地址,但在读取时需要传递数组指针的地址,因为您还不知道要分配多少内存
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
#包括
#包括
#包括
#定义SLENG 50//此值在文件格式中使用
typedef结构歌{
字符名[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
国际日期;
}歌曲;
int saveToFile(Song*x,int songCount){//将信息保存到二进制文件中
文件*f=fopen(“array.bin”、“wb”);
如果(f==NULL){
printf(“错误\n”);
返回-1;
}
fwrite(songCount,sizeof(int),1,f);
int write=fwrite(x,sizeof(struct Song),songCount,f);
fclose(f);
书面回报;
}
int readSong(Song**x,int*songCount){//从文件中读取信息并写入
整数计数=0;
FILE*fr=fopen(“array.bin”、“rb”);
如果(fr==NULL){
printf(“错误\n”);
返回-1;
}
printf(“歌曲:\n”);
fread(&count,sizeof(int),1,fr);
*x=calloc(计数,sizeof(歌曲));
如果(*x==NULL){
printf(“无法分配%d字节的内存,\n”,计数);
fclose(fr);
返回-1;
}
int found=fread(*x,sizeof(struct Song),count,fr);
for(int i=0;i
欢迎使用Stack Overflow!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看程序偏离预期的地方。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:。指针特定于si单进程。你不能保存指针。即使是同一个程序,也无法将指针加载到另一个进程中。要么使用数组,要么想出一种方法来访问每个结构中的数据。是的,指针就是你保存的全部。至于数组,你为每个字符串分配SLENG
字符,总是和非字符串传统上。这实际上并不比拥有一个编译时固定大小的数组好。您可能希望将字符串直接存储到结构中,如typedef struct Song{char name[SLENG];char nameSong[SLENG]
而不是指针。顺便说一句,您的注释#define SLENG 50//仅仅是一个随机值
是不合适的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 // this value is used in the file format
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
int saveToFile(Song *x, int songCount) { //Saves info to the binary file
FILE *f = fopen("array.bin", "wb");
if (f == NULL) {
printf("Error\n");
return -1;
}
fwrite(songCount, sizeof(int), 1, f);
int written = fwrite(x, sizeof(struct Song), songCount, f);
fclose(f);
return written;
}
int readSong(Song **x, int *songCount) { //Reads info from the file and writes it
int count = 0;
FILE *fr = fopen("array.bin", "rb");
if (fr == NULL) {
printf("Error\n");
return -1;
}
printf("Songs:\n");
fread(&count, sizeof(int), 1, fr);
*x = calloc(count, sizeof(Song));
if (*x == NULL) {
printf("Cannot allocate %d bytes of memory\n", count);
fclose(fr);
return -1;
}
int found = fread(*x, sizeof(struct Song), count, fr);
for (int i = 0; i < found; i++) {
printf("%d. %s %s %s %d\n", i + 1,
(*x)[i].name, (*x)[i].nameSong, (*x)[i].timeSong, (*x)[i].date);
}
fclose(fr);
return *songCount = found;
}
void insertSong(Song *x, int Count) { //Inserts new song into the array.
printf("\nInsert name of the band:\n");
scanf("%49s", x[Count].name);
printf("Insert name of the song:\n");
scanf("%49s", x[Count].nameSong);
printf("Insert length of the song:\n");
scanf("%49s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
int main(void) {
int songCount, menuOption;
Song *x = NULL;
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch (menuOption) {
case 1:
printf("Insert how many songs do you want to input?\n");
if (scanf("%d", &songCount) == 1) {
x = calloc(songCount, sizeof(Song));
for (int i = 0; i < songCount; i++) {
insertSong(x, i);
}
saveToFile(x, songCount);
}
break;
case 2:
readSong(&x, &songCount);
break;
}
free(x);
x = NULL;
return 0;
}