从二进制文件存储和打印链接列表指针内容

时间:2019-03-10 13:44:24

标签: c pointers printing linked-list

用户要输入命令,然后将整数添加到链表中,然后将其保存到文件中,最后将该文件的内容打印出来。虽然我已经从本质上说可以运行它,但是有一个问题是程序正在运行时不起作用,因此,尽管它确实添加到了文件中,但在运行时却没有这样做。因此我可以使用命令s,然后使用命令l来查看刚保存到文件中的新内容。我怎样才能做到这一点?另外,我该如何检测文件中是否已包含内容,如果是,则首先打印,

背包壳

#include <stdio.h>
#include "knapsack.c"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 1000
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 buffer[BUFFER_SIZE];
    char *error;
    int totalRead = 0;
   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);
                KnapsackPrint2(&k2, pf);
                fprintf(stdout, "stored in file \"%s\"\n", strtok(input+i, "\n"));
            }
            continue;
        }
        if(command == 'l'){
            if((pf = fopen(input + i, "r")) != NULL){
                error = input + i;
                removeSpaces(error);
                fprintf(stdout, "loaded from file \"%s\"\n", strtok(input+i, "\n"));

                while(fgets(buffer, BUFFER_SIZE, pf) != NULL){
                    totalRead = strlen(buffer);

                    buffer[totalRead -1] = buffer[totalRead -1] == '\n' ? '\0' : buffer[totalRead -1];

                    printf("%s\n", buffer);
                }

            }
            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, FILE* fp){
   if(*knapsack==NULL)
       printf("knapsack: \n");
   else{
       listitemptr temp=*knapsack;
       fprintf(fp, "knapsack:");
       while(temp!=NULL){
           if((temp->next) == NULL){
               fprintf(fp, " %d (%d)", temp->item, temp->count);
               break;
           }else{
           fprintf(fp, " %d (%d),",temp->item,temp->count);
           temp=temp->next;
         }

       }
       printf("\n");
       return temp;
   }
}

0 个答案:

没有答案