Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:27+00:00 2026-05-27T13:13:27+00:00

Please help me, it reads data from a file.. and uses FIFO replacement algo..

  • 0

Please help me, it reads data from a file.. and uses FIFO replacement algo..
I don’t know what’s wrong why did the string (when comparing) contains the smiley character when I made sure I put a null terminator..

example file content:

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

Try to run it…

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:13:28+00:00Added an answer on May 27, 2026 at 1:13 pm

    One problem I can see is:

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

    which is incorrect. Since index in C are 0 based, 8 is not a valid index.

    Looks like you want a char array to hold 8 characters(apart from the NUL char). In that case you will have to declare it to have size 9

    Also looks like you are copying this string addr into structure member DATA. So you’ll have to change the size of DATA char array from 8 to 9 aswell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page that accepts an excel file, reads data from it
I am writing a FORTRAN program that reads data from a text file and
i have a web service that reads data from excel using Microsoft.Office.Interop.Excel. It works
I need to read and print data from a file. I wrote the program
How to add data to dictonary from xml file scenerio: I've declared a dictonary
In my program, I am trying to read data from binary file, and then
my application reads bigram collocation (pairs) from a .txt file. they are to be
I need help with jQuery and ajax. need to load textarea content from file
I am using Microsoft text driver to read data from a CSV file in
New to jQuery, I have a script that returns data from an XML file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.