- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想 fwrite()
然后 fread()
以下 struct **
其内存已动态分配。该结构声明如下:
typedef struct {
int num;
char type;
Entity entity;
}Cell;
我声明一个单元格映射如下:
typedef struct {
char name[MAX_STRING];
int width;
int height;
Cell** map;
}Maze;
我分配 map 的内存如下:
maze.map = (Cell **)malloc( width*sizeof( Cell* ));
for (int x = 0; x < width; x++ )
{
maze.map[x] = (Cell *)malloc(sizeof( Cell )*height);
}
问题是我不能像这样fwrite()
/fread()
我的结构因为一旦我想fread()
struct
,我将无法分配适当的内存空间。所以我决定先写高度和宽度然后分配内存然后读取 map 。但是我找不到如何正确地书写或阅读我的 map 。我试过了:
for (int x = 0; x < maze.width; ++x) {
for (int y = 0; y < maze.height; ++y) {
fwrite(&maze.map[x][y], sizeof(Cell), 1, file);
}
}
但它不起作用,否则我不知道我应该如何写/读我的 map 。
我尝试的一切都没有给我写的 map ,或者它只是崩溃并告诉我:进程已完成,退出代码为 -1073741819 (0xC0000005)
下面是写和读两个函数:
void SaveMaze(Maze maze){
FILE *file;
char file_name[MAX_STRING];
strcpy(file_name, maze.name);
strcat(file_name,".save");
file = fopen(file_name, "w");
if(file == NULL)
{
perror("Error Fopen : ");
return ;
}
fwrite(maze.name,sizeof(maze.name), 1, file);
fwrite(&maze.height,sizeof(maze.height), 1, file);
fwrite(&maze.width,sizeof(maze.width), 1, file);
for (int x = 0; x < maze.width; ++x) {
fwrite(&maze.map[x], sizeof(Cell), maze.height, file);
}
// Close the file
fclose(file);
}
Maze LoadMaze(char * name){
FILE *file;
Maze maze;
char file_name[MAX_STRING];
strcpy(file_name, name);
strcat(file_name,".save");
file = fopen(file_name, "r");
if(file == NULL) {
perror("Error Fopen : ");
return maze;
}
fread(maze.name,sizeof(maze.name), 1, file);
fread(&maze.height,sizeof(maze.height), 1, file);
fread(&maze.width,sizeof(maze.width), 1, file);
maze.map = (Cell **)malloc( maze.width*sizeof( Cell* ));
for (int x = 0; x < maze.width; x++ )
{
maze.map[x] = (Cell *)malloc(sizeof( Cell )*maze.height);
}
for (int x = 0; x < maze.width; ++x) {
fread(&maze.map[x], sizeof(Cell), maze.height, file);
}
printf("%c",maze.map[0][0].num);
fclose (file);
return maze;
}
int main() {
int choice;
int width,height;
char name[MAX_STRING],file_name[MAX_STRING];
Maze maze;
do{
printf("1. Generate a new Maze\n");
printf("2. Load an existing maze\n");
printf("3. Play\n");
printf("4. Exit\n");
scanf("%d",&choice);
fflush(stdin);
if(choice == 1){
do {
printf("What is the width of your maze (Odd number only)\n");
scanf("%d", &width);
fflush(stdin);
} while (width%2 == 0);
do {
printf("What is the height of your maze (Odd number only)\n");
scanf("%d", &height);
fflush(stdin);
} while (height%2 == 0);
printf("What is the name of the maze\n");
fgets(name,sizeof(name),stdin);
// Remove the \n from the name
name[strcspn(name, "\n")] = 0;
fflush(stdin);
maze = CreateMaze(width,height,name);
InitialyzeMaze(&maze);
BuildMaze(&maze);
fflush(stdin);
SaveMaze(maze);
}else if(choice == 2){
//system("clear");
//system("ls *.{save}");
printf("What is the name of the maze you want to load ?\n");
fgets(file_name,sizeof(file_name),stdin);
// Remove the \n from the filename
file_name[strcspn(file_name, "\n")] = 0;
fflush(stdin);
maze = LoadMaze(file_name);
}else if(choice == 3){
Play(&maze);
}
}while(choice != 4);
最佳答案
您在数据文件中混合了文本数据和二进制数据。
当你写名字、高度和宽度时:
fprintf(file,"%s",maze.name);
fwrite(&maze.height,sizeof(maze.height), 1, file);
fwrite(&maze.width,sizeof(maze.width), 1, file);
这会输出一系列名称字符(比如“my_maze”),紧接着是 sizeof(int)
字节的高度和 sizeof(int)
字节对于宽度。所以名称有 7 个字节,高度有 4 个字节(假设 int
是 4 个字节),宽度有 4 个字节。
现在当你回读时:
fscanf(file,"%s",maze.name);
fread(&maze.height,sizeof(maze.height), 1, file);
fread(&maze.width,sizeof(maze.width), 1, file);
fscanf
的%s
格式说明符读取字符,直到遇到空格。前 7 个字符被正确读取,但紧随其后的是 height
的二进制数据,那么它在哪里停止读取?结果是您读取的字节很可能比缩进的多,现在您读取的其余部分不在正确的位置。
您可以通过使用 fwrite
编写整个 name
字段来消除 fprintf
和 fscanf
来解决此问题:
fwrite(maze.name,sizeof(maze.name), 1, file);
并使用 fread
读取它:
fread(maze.name,sizeof(maze.name), 1, file);
你这里也有问题:
fwrite(&maze.map[x], sizeof(Cell), maze.height, file);
在这里:
fread(&maze.map[x], sizeof(Cell), maze.height, file);
&maze.map[x]
不是你分配的内存地址,而是它指向的指针的地址。因此,不是读取/写入为每行单元格预留的内存,而是读取/写入用于每个单元格的指针行的内存。执行此操作时,您最终会读取/写入已分配内存的末尾。
去掉此处的寻址运算符,将指针传递给您正在读/写的实际内存:
fwrite(maze.map[x], sizeof(Cell), maze.height, file);
...
fread(maze.map[x], sizeof(Cell), maze.height, file);
关于c - Fwrite/Fread 动态声明的结构 **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273944/
我遇到一些 fwrite 问题,它以我不明白的方式失败。 在这段代码中,fwrite 都失败并给出 tmp = 0。 if ((file = fopen(filenameout, "wb")) !
好吧,事情是这样的……我记得上周创建了一个程序,要求我以二进制模式打开一个文件并向其中写入数据。首先,我尝试使用fopen函数,检查结果是否正确,然后尝试写入数据。我记得第一次尝试时,fwrite 操
早上好, 我正在尝试将以下结构的内容写入文件(更具体地说是 BMP 文件),并且想知道为什么 fwrite 的二进制输出ing 整个结构和 fwrite结构体的各个项目是不同的。这就是C的工作方式吗?
我在某个地方遇到了这个。 正在写入一个文件,然后rewind(fileptr) 完成,然后写入应该写入文件开头的内容。但是写在开头,不会覆盖文件的内容吗?请指教。 最佳答案 技术上,是 是的,文件将被
fwrite 一个整数取决于字节序,但是有没有一种方法可以将一个整数 0x00000004 写入一个文件,这样无论它运行在什么机器上,它都可以始终被读取为 0x00000004。 一个想法是始终按照特
我在 php 中编写一个函数,在客户端我有一个 Canvas 图像,我使用 toDataUrl() 和一个文件名将图像保存在服务器上。这是代码: 关键是这段代码有效。对于我在其上使用的三分之二的页面
当我运行这个小代码并在控制台上输入 3 个整数时,由于 fwrite 语句,它没有按应有的方式打印出来。只有在我连续按回车键一段时间后,它才会打印出来。帮助?P.S:学习速度比 scanf 和 pri
我的网站在 /var/www/html/hs/ ,我有我的更新.php .它应该写到/var/www/html/hs/json/myFile.json ,但事实并非如此。当我尝试写入 的同一目录时更新
这个函数应该获取一个参数作为文件的指针并将所有文件放入struct anagram ,然后将其写入另一个文件。现在每个数据之间都有很大的空间。 charCompare 工作正常,因为我制作了一个测试文
这是在我的网站上上传带有“var$”内容的“file.txt”的 URL: http://www.mywebsite.com/fwrite.php?stringData=var$&myFile=fil
如果字符串包含 ~,我似乎无法将其打印到标准输出。 > A = "/.git". > io:fwrite(A). /.gitok > B = "~/.git". > io:fwrite(B). **
我发现当我尝试写入一些大数据时 fwrite 失败,如下面的代码所示。 #include #include int main(int argc, char* argv[]) { int s
我正在写这个: $fh = fopen('public/newsletter.txt', 'w'); foreach($entries as $row) { fwrite($fh,
我又来了,这次有一个 PHP 问题...我正忙于制作某种管理面板(非常基本),但是当我试图像我的 slider 或其他东西一样进行更改时,fwrite 会覆盖所有内容。这是我的 PHP 代码: 0)
根据fwrite的手册页: fread() and fwrite() return the number of items successfully read or written (i.e., no
我只是想对一组数字进行排序(虽然我发布的代码不是我要使用的方法,但我确实需要将代码中的输出写入二进制文件). 一切正常,除了“ordenar”的最后一次迭代:当我在订购后再次打印回数字时,除了最后一行
我想使用 data.table::fwrite 以文本日志的形式快速存储和检索状态。这些是通过移动应用程序更新的,该应用程序使用管道工 API 调用到 R 端点。移动应用程序每秒可能会触发许多 API
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 5 年前。 Improve th
我正在尝试使用“fwrite”并制作 snd 文件。我想做 IIR 滤波器。我制作了一个 FIR 滤波器,并使用 IIR 滤波器的代码。(当然,改变系数)但我认为“fwrite”不起作用。因为IIR滤
我正在尝试将 wchar 数组写入 C 中的文件,但是存在某种损坏和不相关的数据,例如这样的变量和路径 c.:.\.p.r.o.g.r.a.m. .f.i.l.e.s.\.m.i.c.r.o.s.o.
我是一名优秀的程序员,十分优秀!