void file_save(void) { printf("saving"); for (int i = 0; i < 6; i++) { Sleep(200); printf("."); } Sleep(200); file_first = fopen("File_First.txt", "w"); fprintf(file_first, "%5d", file_number); fclose(file_first); current = head; for (int i = 1; i <= file_number; i++) { sprintf(file_name, "File_%d.txt", i); file_current = fopen(file_name, "w"); if (file_current == NULL) { printf("No file existed\n"); system("pause"); system("cls"); } else { current = current->next; rewind(file_current); fprintf(file_current, "%5d%5d%5d%5d%5d%5d%5d%5d\n", current->name_num, current->title_num, current->iden_num, current->pro_num, current->abi_num,current->race_num, current->item_num, current->soc_num); name_current = current->_name; title_current = current->_title; iden_current = current->_iden; pro_current = current->_pro; abi_current = current->_abi; race_current = current->_race; item_current = current->_item; soc_current = current->_soc; for (int i = 0; i < current->name_num; i++) { name_current = name_current->next; fprintf(file_current,"%s\n", name_current->name); } for (int i = 0; i < current->title_num; i++) { title_current = title_current->next; fprintf(file_current,"%s\n", title_current->title); } for (int i = 0; i < current->iden_num; i++) { iden_current = iden_current->next; fprintf(file_current,"%s\n", iden_current->identity); } for (int i = 0; i < current->pro_num; i++) { pro_current = pro_current->next; fprintf(file_current, "%s\n%d\n", pro_current->profession, pro_current->level); } for (int i = 0; i < current->abi_num; i++) { abi_current = abi_current->next; fprintf(file_current, "%s\n%s\n", abi_current->ability, abi_current->explain); } for (int i = 0; i < current->race_num; i++) { race_current = race_current->next; fprintf(file_current, "%s\n", race_current->race); } for (int i = 0; i < current->item_num; i++) { item_current = item_current->next; fprintf(file_current, "%s\n%s\n%d\n", item_current->item, item_current->explain, item_current->number); } for (int i = 0; i < current->soc_num; i++) { soc_current = soc_current->next; fprintf(file_current, "%s\n%s\n%s\n", soc_current->object, soc_current->relationship, soc_current->explain); } fclose(file_current); } } system("files have been saved\n"); system("pause"); system("cls"); } 为什么最后保存的时候保存顺序有很大差别,假设已经有5个文件,为什么不是从File_1保存到File_5
时间: 2024-04-17 09:23:10 浏览: 152
qishi.rar_Step Up I
根据你提供的代码,最后保存的顺序是根据链表中节点的顺序来进行保存的。在保存文件之前,你使用了一个循环 `current = current->next` 来遍历链表,这将导致在每次保存文件时,当前节点都会递增到下一个节点。因此,最终保存的文件顺序是根据链表中节点的顺序来决定的。
如果你想按照特定的顺序保存文件,你可以修改代码以满足你的需求。例如,你可以在循环之前定义一个数组来存储要保存的文件顺序,然后根据这个顺序遍历链表并保存文件。
另外,请注意,在循环中使用了相同的变量名 `i`,这可能会导致代码逻辑错误。建议在每个循环中使用不同的变量名来避免混淆。
阅读全文