printf()打印垃圾字符

时间:2011-12-13 05:57:08

标签: c string printf

请帮助我,它从文件中读取数据..并使用FIFO替换算法.. 我不知道为什么字符串(当比较时)包含笑脸字符时,我确定我放置了一个空终止符是什么错误。

示例文件内容:

load AAAAAAAA
load AAAAAAAA
load BBBBBBBB
load BBBBBBBB
load BBBBBBBB
load BBBBBBBB
load AAAAAAAA
load CCCCCCCC
load CCCCCCCC
load DDDDDDDD
load EEEEEEEE
load EEEEEEEE
load FFFFFFFF

尝试运行它......

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

FILE *fpointer;

typedef struct cache Cache;
struct cache{
    char DATA[8];
    int COUNT;
    int LOAD;
    Cache *NEXT;
};

int scan(char *addr, Cache *front){
    while(front != NULL){
        printf("compare: %s & %s\n", addr, front->DATA);
        if(strcmp(addr, front->DATA) == 0){
            front->COUNT++;
            return 0;
        }
        front->LOAD++;
        front = front->NEXT;
    }
    return 1;
}


void FirstInFirstOut(int x){
    Cache *ITEM[x];
    Cache *front, *head;
    Cache *node;
    char addr[8]="";
    int i=0, j=0, k=0, m=0;

    for(i=0; i<x; i++){
        ITEM[i] = NULL;
    }

    front = NULL;
    head = NULL;

    while(!feof(fpointer)){
        fscanf(fpointer, "load %c%c%c%c%c%c%c%c\n", &addr[0],&addr[1],&addr[2],&addr[3],&addr[4],&addr[5],&addr[6],&addr[7]);
        addr[8] = '\0';
        printf("read item: %s\n", addr);

        if(front != NULL){
            if(scan(addr, front)==0)
                continue;
        }

        node = (Cache*)malloc(sizeof(Cache));
        strcpy(node->DATA, addr);
        node->COUNT = 0;
        node->LOAD = 0;
        node->NEXT = NULL;
        if(head == NULL){
            head = node;
            front = node;
        }else{
            head->NEXT = node;
            head = node;
        }


        if(j<x){
            ITEM[j] = node;
            printf("insert... %s\n", ITEM[j]->DATA);
        }else{
            m = j%x;          //get the new index
            free(ITEM[m]);
            ITEM[m] = node;
            printf("insert... %s\n", ITEM[m]->DATA);
        }
        j++;

    }

    printf("Cache itmes:\n");
    for(i=0; i<x; i++){
        printf("Item %d: ", i+1);
        if(ITEM[i]->DATA == NULL){
            printf("NULL\n");
        }else{
            for(k=0; k<8; k++){
                printf("%c", ITEM[i]->DATA[k]);
            }
            printf("\n");
        }
        free(ITEM[i]);
    }

}

int main(){
    char fname[20]="", algo[5]="";
    int entries=0;

    printf("Input file:\n");
    scanf("%s", fname);
    printf("Number of cache items:\n");
    scanf("%d", &entries);
    printf("Replacement algorithm:\n");
    scanf("%s", algo);

    if((fpointer = fopen(fname,"r")) == NULL){
        printf("Error opening file.\n");
        return main();
    }

    if(entries == 0){
        printf("Nothing in the cache.\n");
        return main();
    }

    if(strcmp(algo, "FIFO") == 0){
        FirstInFirstOut(entries);
    }


    return main();

}

1 个答案:

答案 0 :(得分:7)

我能看到的一个问题是:

char addr[8]="";
....
addr[8] = '\0'; 

这是不正确的。由于C中的索引基于0,因此8不是有效索引。

看起来你想要一个char数组来保存8个字符(除了NUL字符之外)。在这种情况下,您必须声明它的大小为9

看起来您正在将此字符串addr复制到结构成员DATA中。因此,您必须将DATA字符数组的大小从8更改为9