Thread: Read From a file

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    21

    Read From a file

    I'm having trouble figuring out how to read from a file in a specific way. My input file looks something like this: where 10 is the number of processes and the next lines will be arrival time and burst time.
    10
    23 25
    7 16
    6 6
    8 26
    3 47
    29 22
    1 29
    11 16
    14 41
    24 4

    My code looks like for now:
    Code:
    struct processStruct
    {
        int ID;
        int arrivalTime;
        int burstTime;
    };
    
    
    void file()
    {
        FILE *fp;
        
        fp = fopen("sample1.txt", "r");
        
        if(fp == NULL)
        {
            perror("Error opening the file\n");
            exit(EXIT_FAILURE);
        }
        
        fclose(fp);
           
        //return 0;
    
    
    }
    
    
    int main()
    {
        file();
        menu();
        return 0;
    }
    
    
    void menu()
    {
        int input;
        
        printf("Please Select which scheduling Algorithm you would like to use\n");
        printf("1. First Come First Serve\n");
        printf("2. Round Robin\n");
        printf("3. Exit\n");
        scanf("%d", &input);
        
        switch(input)
        {
            case 1:
                fcfs();
                //function goes here
                break;
            case 2:
                //function
                break;
            case 3:
                printf("Exiting...\n");
                break;
            default:
                printf("Please choose a number 1 to 3\n");
                break;
        }
        getchar();
    }
    
    
    
    
    void fcfs()
    {
        
    }
    
    
    void rr()
    {
        
    }
    Any help is appreciated thank you
    Last edited by Zante; 11-30-2017 at 11:04 AM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    so you're saying you need ten input values of one type then comes a different type? if yes, put it in a loop then set it up to switch to getting your other data needed when the appropriate time arrives.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Kind of yea. First line 10 would declare the number of processes so you would have to print P[1]-P[10] then the next lines print out arrival times and burst times. I'm confused on how you would do those.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first thing you need is
    Code:
    struct processStruct processes[10];
    Now update file() to read lines from the file, and assign things to your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Zante View Post
    Kind of yea. First line 10 would declare the number of processes so you would have to print P[1]-P[10] then the next lines print out arrival times and burst times. I'm confused on how you would do those.
    Forgive me for sounding like this, but, You do not even have anything other then opening and closing your file in your file function. Am I to assume that do not have any idea how to declare a variable, how to assign a value to it. how to use loops to control how many times it "loops". How to use arrays in a loop to get input and how to use a loop to print that out put from the array?

    Patrice opening a file then putting it into a loop to extract needed data out of that file that is set in a specif manner so you know of so that you can use, say fscanf to extract the same data in the specif order that it is put into that file.

    if you already know, which you should is how to put an array in a loop and add values to it, then using a struct is no different. You know the order of data type you put into the struct.

    Just put these basics concepts together in the order/steps in which you need it to be done.

    open file, read data.
    loop to eof adding data into your struct array.
    close file, deal with your array accordingly using the means to control your loop as well. you already know when the "other" data is suppose to show up on the 10th occurrence or is it the 11th?
    you should already know how to check for values to be true or not, and how to make a loop loop X amount of times before it stops.
    then where the program looks next to see if their is something else it needs to do before ending.

    Do not be afraid to blow up your program while you're trying to figure out how to get it to do what you want it to do.

    post back some aggressive progress so I and the others can help you out.

    Last edited by userxbw; 11-30-2017 at 12:00 PM.

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Alright heres my updated code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    struct processStruct
    {
        int arrivalTime;
        int burstTime;
        int waitingTime;
        int turnaroundTime;
    };
    
    
    void fcfs();
    void file(int x);
    
    
    struct processStruct processes[10];
    
    
    void file(int x)
    {
        int n;
        int i = 0;
        //int x = 0;
        
        FILE *fp;
        
        fp = fopen("sample1.txt", "r");
        
        if(fp == NULL)
        {
            perror("Error opening the file\n");
            exit(EXIT_FAILURE);
        }
        
        fscanf(fp, "%d", &x);
        
        while(fscanf(fp, "%d\t%d", &processes[i].arrivalTime, &processes[i].burstTime) != EOF)
        {
            printf("Process ID: [%d] \t Arrival Time: %d \t Burst Time: %d\n", i, processes[i].arrivalTime, processes[i].burstTime);
            i++;
        }
        
        fclose(fp);
        
    }
    
    
    int main()
    {
        int input;
        int x = 0;
        
        printf("Please Select which scheduling Algorithm you would like to use\n");
        printf("1. First Come First Serve\n");
        printf("2. Round Robin\n");
        printf("3. Exit\n");
        scanf("%d", &input);
        
        switch(input)
        {
            case 1:
                file(x);
                fcfs();
                break;
            case 2:
                //function
                break;
            case 3:
                printf("Exiting...\n");
                break;
            default:
                printf("Please choose a number 1 to 3\n");
                break;
        }
        getchar();
    }
    
    
    
    
    void fcfs()
    {
        int n,avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
        
        processes[0].waitingTime = 0;
        // Calculating Waiting and Response Time
        for(i=1;i<n;i++)
        {
            sum = sum + processes[i-1].burstTime;
            processes[i].waitingTime = sum - processes[i].arrivalTime;
            if(processes[i].waitingTime < 0)
                processes[i].waitingTime = 0;
        }
        
        printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
        
        // Calculating Turn Around Time and Printing the Table
        for(i=0;i<n;i++)
        {
            processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime;
            avwt += processes[i].waitingTime;
            avtat += processes[i].turnaroundTime;
            printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime);
        }
        
        avwt/=i;
        avtat/=i;
        printf("\n\nAverage Waiting Time:%d",avwt);
        printf("\nAverage Turnaround Time:%d\n\n",avtat);
        
        
    }
    
    
    
    
    void rr()
    {
        
    }
    But when i run it I get a waiting time of 0 for the first process which is wrong so all the turnaround times are wrong. Its supposed to be waiting time of 184 and turnaround time of 209 so where did i go wrong with that?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you're making a very bad job of returning the value of x from file().

    So in main, x is still 0.

    And in fcfs, your value of n controlling the loop is garbage.
    > int n,avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
    > for(i=1;i<n;i++)
    n is uninitialised here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    struct processStruct
    {
        int arrivalTime;
        int burstTime;
        int waitingTime;
        int turnaroundTime;
    };
    
    
    void fcfs();
    void file();
    
    
    struct processStruct processes[10];
    
    
    void file()
    {
        int n;
        int i = 0;
        int x = 0;
        
        FILE *fp;
        
        fp = fopen("sample1.txt", "r");
        
        if(fp == NULL)
        {
            perror("Error opening the file\n");
            exit(EXIT_FAILURE);
        }
        
        fscanf(fp, "%d", &x);
        
        while(fscanf(fp, "%d\t%d", &processes[i].arrivalTime, &processes[i].burstTime) != EOF)
        {
            printf("Process ID: [%d] \t Arrival Time: %d \t Burst Time: %d\n", i, processes[i].arrivalTime, processes[i].burstTime);
            i++;
        }
        
        fclose(fp);
        
    }
    
    
    int main()
    {
        int input;
        
        printf("Please Select which scheduling Algorithm you would like to use\n");
        printf("1. First Come First Serve\n");
        printf("2. Round Robin\n");
        printf("3. Exit\n");
        scanf("%d", &input);
        
        switch(input)
        {
            case 1:
                file();
                fcfs();
                break;
            case 2:
                //function
                break;
            case 3:
                printf("Exiting...\n");
                break;
            default:
                printf("Please choose a number 1 to 3\n");
                break;
        }
        getchar();
    
    
        return 0;
    }
    
    
    
    
    void fcfs()
    {
        int n = 0;
        int avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
        
        processes[0].waitingTime = 0;
        // Calculating Waiting and Response Time
        for(i = 1; i < n; i++)
        {
            sum = sum + processes[i - 1].burstTime;
            processes[i].waitingTime = sum - processes[i].arrivalTime;
            if(processes[i].waitingTime < 0)
                processes[i].waitingTime = 0;
        }
        
        printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
        
        // Calculating Turn Around Time and Printing the Table
        for(i = 0; i < n; i++)
        {
            processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime;
            avwt += processes[i].waitingTime;
            avtat += processes[i].turnaroundTime;
            printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime);
        }
        
        avwt/=i;
        avtat/=i;
        printf("\n\nAverage Waiting Time:%d",avwt);
        printf("\nAverage Turnaround Time:%d\n\n",avtat);
        
        
    }
    
    
    
    
    void rr()
    {
        
    }
    This causes a floating point exception: 8

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Great, you initialise n to 0, so how many times do your loops run now, and what value will i be when it hits the divisions.

    What happened to returning x from file() to main() ?Thanks
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    On your issue with var n
    Code:
    for(i = 0; i < n; i++)
    this may jog your memory
    : where 10 is the number of processes and the next lines will be arrival time and burst time.
    I do believe that is what you're looking for. Also you have a LOT of unused variables everywhere in your code. You might want to clean that up if you're not going to be using them.

    Just a little programming tip if (after you remember how many times you where suppose to loop your data before giving your results / output)

    It can be used when getting an unknown amount of data from a file ( or elsewhere, or if you just happen to forget what n is suppose to be.
    To use in a loop or for something else. This is just an example to show the methodology. If applied properly it can work to your advantage.

    Code:
    #include <stdio.h>
    int Return_value_gotten(void)
    {
        int x = 0;
        
        for ( ; x < 3; x++)
        {
            ;
        }
        return x;    
    }
    int main (void)
    {
    
    int g = 0;
    for (; g < Return_value_gotten(); g++)    
        printf("%d\n",g);
    return 0;
    }
    output
    Code:
    $ ./use_retrun_in_loop
    0
    1
    2
    Last edited by userxbw; 12-01-2017 at 09:58 AM.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    OK call it a pet peeve if you want to but your program design was really bothering me. Hopefully it will help you to learn how to pass variables in your functions, and localization. So you can use your functions to do the work for you to make the program run smoother/faster/more efficient. pick one or all the above.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct processStruct
    {
        int arrivalTime;
        int burstTime;
        int waitingTime;
        int turnaroundTime;
    };
    
    void fcfs(struct processStruct processes[], int n)
    {
        
        int avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
         
        processes[0].waitingTime = 0;
        // Calculating Waiting and Response Time
        for(i = 1; i < n; i++)
        {
            sum = sum + processes[i - 1].burstTime;
            processes[i].waitingTime = sum - processes[i].arrivalTime;
            if(processes[i].waitingTime < 0)
                processes[i].waitingTime = 0;
        }
         
        printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
         
        // Calculating Turn Around Time and Printing the Table
       // for(i = 0; i < 10; i++)
        for(i = 0; i < n; i++)
        {
            processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime;
            avwt += processes[i].waitingTime;
            avtat += processes[i].turnaroundTime;
            printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime);
        }
         
        avwt/=i;
        avtat/=i;
        printf("\n\nAverage Waiting Time:%d",avwt);
        printf("\nAverage Turnaround Time:%d\n\n",avtat);
         
         
    }
    void rr()
    {
         
    }
    int file(struct processStruct processes[])
    {
        
        int i = 0;
        int x = 0;
         
        FILE *fp;
         
        fp = fopen("zante_test_file", "r");
         
        if(fp == NULL)
        {
            perror("Error opening the file\n");
            exit(EXIT_FAILURE);
        }
         
        fscanf(fp, "%d", &x);
         
        while(fscanf(fp, "%d\t%d", &processes[i].arrivalTime, &processes[i].burstTime) != EOF)
        {
            printf("Process ID: [%d] \t Arrival Time: %d \t Burst Time: %d\n", i, processes[i].arrivalTime, processes[i].burstTime);
            i++;
        }
         
        fclose(fp);
      return i;
    }
     
     
    int main()
    {
        struct processStruct processes[10];
     
        int input;
         
        printf("Please Select which scheduling Algorithm you would like to use\n");
        printf("1. First Come First Serve\n");
        printf("2. Round Robin\n");
        printf("3. Exit\n");
        scanf("%d", &input);
         
        //after selection open file and read it
        int amount_of_data = file(processes);
        switch(input)
        {
            case 1: // call to file() can be added here also if you want to eliminate the use of one int. 
                      // or if you only want to open that file on selection # 1
                // fcfs(processes,file(processes));
                fcfs(processes,amount_of_data );
                break;
            case 2:
                //function
                break;
            case 3:
                printf("Exiting...\n");
                break;
            default:
                printf("Please choose a number 1 to 3\n");
                break;
        }
        getchar();
     
     
        return 0;
    }
    also if your functions are written out above main you do not have to add the prototypes above main.
    Last edited by userxbw; 12-01-2017 at 10:51 AM.

  12. #12
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Alright so I cleaned up the code like you suggested, got rid of all the unused variables. I'm still confused on where or what I'm doing wrong in my fcfs function where I get the output of: Process Arrival Time Burst Time Waiting Time Turnaround Time
    P[0] 25 25 0 25

    and I'm supposed to get 25 25 184 209. I'm thinking I need to sort somehow but not exactly sure how to at this moment.

    Code:
    for(i = 0; i < n; i++)
        {
            pos = i;
            for(j = i + 1; j < n; j++)
            {
                if(processes[j].arrivalTime < processes[pos].arrivalTime)
                {
                    pos = j;
                }
            }
            
            temp = processes[i].arrivalTime;
            processes[i].arrivalTime = processes[pos].arrivalTime;
            processes[pos].arrivalTime = temp;
            
            temp = processes[i].burstTime;
            processes[i].burstTime = processes[pos].burstTime;
            processes[pos].burstTime = temp;
            
            //sort process id here
    This is an idea that i think might work?
    Last edited by Zante; 12-01-2017 at 11:08 AM.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    if you print out your math it might help you to see what is going on. Put your printf where needed to see what is going on.
    did you take a look at post 11 to see that rewrite of your code?
    Code:
    
    void fcfs(struct processStruct processes[], int n)
    {
        
        int avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
         
        processes[0].waitingTime = 0;
        // Calculating Waiting and Response Time
        for(i = 1; i < n; i++)
        {
            sum = sum + processes[i - 1].burstTime;
            printf("sum = %d\n", sum);
            processes[i].waitingTime = sum - processes[i].arrivalTime;
            printf("i = %d , processes[i].waitingTime %d\n",i, processes[i].waitingTime);
            if(processes[i].waitingTime < 0) {
                processes[i].waitingTime = 0;
                 printf("in if check\ni = %d , processes[i].waitingTime %d\n",i, processes[i].waitingTime);
             }
             //print burst time
            printf("Burst %d\n",  processes[i].burstTime );
        }
         
        printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
         
        // Calculating Turn Around Time and Printing the Table
       // for(i = 0; i < n; i++)
        for(i = 0; i < n; i++)
        {
            processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime;
            avwt += processes[i].waitingTime;
            avtat += processes[i].turnaroundTime;
            printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime);
        }
         
        avwt/=i;
        avtat/=i;
        printf("\n\nAverage Waiting Time:%d",avwt);
        printf("\nAverage Turnaround Time:%d\n\n",avtat);
         
         
    }
    test file I am using
    Code:
    23 25
    7 16
    6 6
    8 26
    3 47
    29 22
    1 29
    11 16
    14 41
    24 4
    post your complete function. I have no idea where your new code goes in that function.
    Last edited by userxbw; 12-01-2017 at 11:19 AM.

  14. #14
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    void fcfs(struct processStruct processes[], int n) { int avwt=0,avtat=0,i,j,pos,max,temp,sum=0; for(i = 0; i < n; i++) { pos = i; for(j = i + 1; j < n; j++) { if(processes[j].arrivalTime < processes[pos].arrivalTime) { pos = j; } } temp = processes[i].arrivalTime; processes[i].arrivalTime = processes[pos].arrivalTime; processes[pos].arrivalTime = temp; temp = processes[i].burstTime; processes[i].burstTime = processes[pos].burstTime; processes[pos].burstTime = temp; //sort process id here processes[0].waitingTime = 0; // Calculating Waiting and Response Time for(i = 1; i < n; i++) { sum = sum + processes[i - 1].burstTime; printf("sum = %d\n", sum); processes[i].waitingTime = sum - processes[i].arrivalTime; printf("i = %d , processes[i].waitingTime %d\n",i, processes[i].waitingTime); if(processes[i].waitingTime < 0) { processes[i].waitingTime = 0; printf("in if check\ni = %d , processes[i].waitingTime %d\n",i, processes[i].waitingTime); } //print burst time printf("Burst %d\n", processes[i].burstTime ); } printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time"); // Calculating Turn Around Time and Printing the Table // for(i = 0; i < n; i++) for(i = 0; i < n; i++) { processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime; avwt += processes[i].waitingTime; avtat += processes[i].turnaroundTime; printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime); } avwt/=i; avtat/=i; printf("\n\nAverage Waiting Time:%d",avwt); printf("\nAverage Turnaround Time:%d\n\n",avtat); }
    sorry on my phone but this is essiantially where it would go

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    are these the results more to what you where looking for?
    Code:
    Process        Arrival Time    Burst Time    Waiting Time    Turnaround Time
    P[0]        23        25        0        25
    P[1]        7        16        18        34
    P[2]        6        6        35        41
    P[3]        8        26        39        65
    P[4]        3        47        70        117
    P[5]        29        22        91        113
    P[6]        1        29        141        170
    P[7]        11        16        160        176
    P[8]        14        41        173        214
    P[9]        24        4        204        208
    
    Average Waiting Time:93
    Average Turnaround Time:116
    if yes then just remove this bit of code as you're scanning your file before your loop so it is causing it to mis read your data file in your file function
    Code:
     // fscanf(fp, "%d", &x);
    well second thought, that was for your 10 I took that out. let me look closer. I already posted this so might as well leave it.
    Last edited by userxbw; 12-01-2017 at 12:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 07:01 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

Tags for this Thread