我试图制作的背包程序有一个接受命令的用户界面,其中之一是保存并从文件中加载。我想知道当在同一行中全部给出此命令时如何通过用户输入创建文件,然后将链接列表的内容存储到此文件中,然后能够加载它并在以后打印出来。使我烦恼的部分原因是将它们全部输入相同的输入,然后将内容打印到该用户定义的文件名中。我应该使用fopen和fprintf吗?同样,如果给定的文件名类似于/ rootfile,则应将其标记为由于目录而无法保存到该文件。它们也应该是二进制文件。
背包壳
#include <stdio.h>
#include "knapsack.c"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void removeSpaces(char *str1)
{
char *str2;
str2=str1;
while (*str2==' ') str2++;
if (str2!=str1) memmove(str1,str2,strlen(str2)+1);
}
int main()
{
listitemptr k2 = NULL;
char input[100];
char command;
int i, returnval = 0;
char filename[250];
char userfile[260];
char *error;
for (;;) {
FILE* pf = NULL;
fprintf(stdout, "> ");
if (!fgets(input, sizeof input, stdin))
break;
i = strspn(input, " \t\n"); /* skip blanks */
command = input[i++];
if (command == 'q' && input[i] == '\n')
break;
if (command == 'p') {
KnapsackPrint(&k2);
continue;
}
if (command == 'a') {
int item;
if (sscanf(input + i, "%i", &item) != 1) {
error = input + i;
removeSpaces(error);
fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));
continue;
}
KnapsackAdd(&k2, item);
KnapsackPrint(&k2);
continue;
}
if (command == 'r') {
int item;
if (sscanf(input + i, "%i", &item) != 1) {
error = input + i;
removeSpaces(error);
fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));
continue;
}
if(KnapsackRemove(&k2, item) == -1){
fprintf(stderr, "Error: item %d does not exist in knapsack\n", item);
continue;
}else{
KnapsackPrint(&k2);
continue;
}
}
if(command == 's'){
if(( pf = fopen(input + i, "a")) != NULL){
error = input + i;
removeSpaces(error);
fprintf(pf, "%s", KnapsackPrint2(&k2));
fprintf(stdout, "stored in file \"%s\"\n", strtok(input+i, "\n"));
}
continue;
}
if(command == 'l'){
continue;
}
else{
fprintf(stderr, "unknown command: %s", input);
fprintf(stdout, "> ");
}
}
return returnval;
}
背包。
#include "knapsack.h"
#include <stdio.h>
#include <stdlib.h>
listitemptr KnapsackAdd(listitemptr *knapsack, int item){
if(*knapsack==NULL){//empty list
listitemptr newest= malloc(sizeof(struct listitem));
newest->item=item;
newest->count=1;
newest->next=NULL;
*knapsack = newest;
return newest;
}else{
listitemptr current=*knapsack;
listitemptr prev=NULL;
while(current!=NULL){
if(current->item == item){
current->count=current->count+1;
break;
}else if(current -> item > item){
listitemptr new_node = malloc(sizeof(struct listitem));
new_node-> item = item;
new_node-> count= 1;
new_node-> next = current;
if(prev != NULL ){
prev->next = new_node;
}else {
*knapsack = new_node;
}
break;
}
prev=current;
current=current->next;
}
if(current==NULL){
listitemptr newest= malloc(sizeof(struct listitem));
newest->item=item;
newest->count=1;
newest->next=NULL;
prev->next=newest;
return newest;
}
return current;
}
}
int KnapsackRemove(listitemptr *knapsack, int item){
if(*knapsack==NULL)
return -1;
listitemptr present=*knapsack;
listitemptr previous=NULL;
while(present!=NULL){
if(present->item==item){
if(present->count>1){
present->count=present->count-1;
}else{
if(previous==NULL){ //delete at head
*knapsack=present->next;
}else{
previous->next=present->next;
free(present);
}
}
break;
}
previous=present;
present=present->next;
}
return 0;
}
void KnapsackPrint(const listitemptr *knapsack){
if(*knapsack==NULL)
printf("knapsack: \n");
else{
listitemptr temp=*knapsack;
printf("knapsack:");
while(temp!=NULL){
if((temp->next) == NULL){
printf(" %d (%d)", temp->item, temp->count);
break;
}else{
printf(" %d (%d),",temp->item,temp->count);
temp=temp->next;
}
}
printf("\n");
}
}
unsigned int KnapsackItemCount(const listitemptr *knapsack, int item){
if(*knapsack==NULL)
return 0;
listitemptr temp=*knapsack;
while(temp!=NULL){
if(temp->item==item)
return temp->count;
temp=temp->next;
}
return 0;
}
unsigned int KnapsackSize(const listitemptr *knapsack){
if(*knapsack==NULL){
return 0;
}
listitemptr temp=*knapsack;
unsigned int sum=0;
while(temp!=NULL){
sum+=temp->count;
temp=temp->next;
}
return sum;
}
listitemptr KnapsackPrint2(const listitemptr *knapsack){
if(*knapsack==NULL)
printf("knapsack: \n");
else{
listitemptr temp=*knapsack;
printf("knapsack:");
while(temp!=NULL){
if((temp->next) == NULL){
printf(" %d (%d)", temp->item, temp->count);
break;
}else{
printf(" %d (%d),",temp->item,temp->count);
temp=temp->next;
}
}
printf("\n");
return temp;
}
}