- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我写了这段代码:代码:https://repl.it/JA1s/4
int list_Size(link_t *anchor);
void menu(link_t *list);
void freeMemory(link_t *list);
void freeLink(link_t *list);
void writeListToFile();
#include "view.h"
#include "linkedList.h"
int main(void) {
link_t *anchor = NULL;
system("COLOR 81");
int choise = 0;
printf("Welcome!\nPress 0 to make a new list\npress any other key to load an existing listing");
scanf("%d", &choise);
if (choise == 0) {
menu(anchor);
return 0;
}
if (choise != 0) {
anchor=readList(anchor);
}
}
void menu(link_t* anchor) {
unsigned int number = 0, choice = 0, num = 0, ans = 0;
char* name = (char*)malloc(MAX_NAME_SIZE);
if (name) {
if (anchor) {
freeMemory(anchor);
return 1;
}
}
while (number == 0) {
do {
printf("Welcome to Magshimim Movie Maker! what would you like to do? \n");
printf("[0] exit\n");
printf("[1] Add new Frame\n");
printf("[2] Remove a Frame\n");
printf("[3] change frame index\n");
printf("[4] Change frame duration\n");
printf("[5] Change duration of all frames\n");
printf("[6] List frames\n");
printf("[7] Play movie!\n");
printf("[8] Save List to file");
scanf("%d", &choice);
getchar();
switch (choice) {
case 0:
freeMemory(anchor);
number = 1;
break;
case 1:
addNewPic(&anchor);
break;
case 2:
printf("Enter the frame's name: ");
fgetsMe(name, MAX_NAME_SIZE);
removePic(&anchor, name);
break;
case 3:
changePosition(&anchor);
break;
case 4:
printf("Enter the frame's name: ");
fgetsMe(name, MAX_NAME_SIZE);
durationSong(anchor, name);
break;
case 5:
printf("enter a num \n");
scanf("%d", &num);
getchar();
durationOfAll(anchor, num);
break;
case 6:
display(anchor);
system("pause");
break;
case 7:
playMenu(anchor);
break;
case 8:
writeListToFile(anchor);
break;
default:
printf("you write a not currect number\n");
}
system("CLS");
} while (choice < 0 || choice > 7);
}
}
void freeMemory(link_t* list) {
link_t* temp = NULL;
while (list) {
temp = list;
list = list->next;
freeLink(temp);
}
}
link_t *readNextFromFile(link_t *anchor, FILE *pfile) {
size_t returnValue;
if (anchor == NULL) {
anchor = malloc(sizeof(link_t));
returnValue = fread(anchor, sizeof(link_t), 1, pfile);
anchor->next = NULL;
anchor->prevoius = NULL;
} else {
link_t *index = anchor;
link_t *neww = malloc(sizeof(link_t));
while (index->next != NULL) {
index = index->next;
}
returnValue = fread(neww, sizeof(link_t), 1, pfile);
index->next = neww;
neww->next = NULL;
neww->prevoius = index;
}
}
link_t *readList(link_t *anchor) {
int loop = 0;
FILE *pfile;
pfile = fopen("s.bin", "rb");
if (pfile != NULL) {
anchor = NULL;
fseek(pfile, 0, SEEK_END);
long filesize = ftell(pfile);//tell what is the size
rewind(pfile); //get back to the start before we start reading it
int numEntries = (int)(filesize / (sizeof(link_t)));
for (loop = 0; loop < numEntries; loop++) {
//looping through the struct
fseek(pfile, (sizeof(link_t)*loop), SEEK_SET);
anchor = readNextFromFile(anchor, pfile);
}
menu(anchor);
} else {
printf("File open error\n");
}
return anchor;
}
void writeListToFile(link_t **anchor) {
FILE *pfile;
pfile = fopen("s.bin", "wb");
if (pfile != NULL) {
link_t *current = anchor;
link_t *prev = NULL;
link_t *next = NULL;
while (current != NULL) {
next = current->next;
prev = current->prevoius;
current->next = NULL;
current->prevoius = NULL;
fseek(pfile, 0, SEEK_END);
fwrite(current, sizeof(link_t), 1, pfile);
current->next = next;
current->prevoius = prev;
next = NULL;
prev = NULL;
current = current->next;
}
fclose(pfile);
} else {
printf("File Open Error!");
pfile = NULL;
}
}
linkedList.h:https://repl.it/JA1s/2
#ifndef LINKEDLISTH
#define LINKEDLISTH
#include "Frame.h"
struct Link {
frame_t *frame;
struct Link *next;
struct Link *prevoius;
};
typedef struct Link link_t;
#endif
view.h:https://repl.it/JA1s/3
#include <opencv2\core\core_c.h>
#include <opencv2\highgui\highgui_c.h>
#include <stdio.h>
#include "linkedList.h"
void play(link_t *list);
框架.h
#ifndef FRAME_H
#define FRAME_H
#include <stdio.h>
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
#endif //FRAME_H
当我在开始时按 0 以外的任何内容来加载带有我创建的链接列表的现有 bin 文件时,我的问题就出现了。我收到错误:在
处出现未处理的异常。是什么原因? while (index->next != NULL){
处的 0x003C1043
最佳答案
您必须以二进制模式打开文件,fread
和 fwrite
才能正确处理二进制数据。这可能不是唯一的问题,但对于 Windows 主机来说绝对是一个主要问题。使用这个:
pfile = fopen("s.bin", "rb");
此外,您无法通过这种方式将此列表保存到磁盘:
您实际上为下一个
和上一个
链接保存了NULL指针,但列表有效负载也是一个指针frame_t *frame;
当您从之前的运行中重新加载其值时,这将无关紧要。
保存程序的当前状态称为序列化。您必须使用独立于内存布局的文件格式。强烈建议使用基于文本的格式(例如 JSON),以便于调试和可移植性。
这是一个以 JSON 格式保存列表的函数:
#include <errno.h>
#include <stdio.h>
#include <string.h>
int writeListToFile(const link_t *list) {
FILE *pfile = fopen("s.json", "w");
if (pfile != NULL) {
fprintf(pfile, "[\n");
for (; list != NULL; list = list->next) {
fprintf(" { "\"name\": \"%s\", \"duration\": %u, \"path\": \"%s\" },\n",
list->frame->name, list->frame->duration, list->frame->path);
}
fprintf(pfile, "]\n");
fclose(pfile);
return 1;
} else {
fprintf(stderr, "Cannot open s.json: %s\n", strerror(errno));
return 0;
}
}
注释:
此函数假设框架成员已正确分配,并且字符串不包含任何特殊字符,例如 "
、\
和换行符。
将其读回内存的函数编写起来有点困难,但一个简单的方法是假设格式是固定的。
关于c - 保存链接列表以供以后使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737813/
命令 svn status 返回如下内容: ? SomeClient\BUTCHERED.docx M SomeClient\Development notes.txt ?
我是一名优秀的程序员,十分优秀!