简体   繁体   中英

Read data from file .txt

I have a c++ code here. This code is to calculate data inside some files.

#include<stdio.h> 
#include<iostream>
#include <fstream>
using namespace std;

int main(){
//initiate file variable
ifstream inFile;
//other variable
int count, limit=30, time, remain, flag=0, time_quantum, arrival_time=30, var, total_process[30], data;
int wait_time=0, turnaround_time=0, rt[30], num_of_interrupts=0, num_of_jobs=0; 


cout<<"Your job list :\n";

for(var = 1; var <= limit; var++){

    //the purpose is the check weither looping is okay or not
    cout<<""<<var;

    //preparing array for the file
    char nambuf[30];

    std::snprintf(nambuf, sizeof(nambuf), "job%d.txt", var);
    std::ifstream inFile;

    //open file
    inFile.open(nambuf);

    //check file 
    if (!inFile) {
        cout << " Unable to open file";
        exit(1); // terminate with error
    }

    //read data from file .txt    
    while (inFile >> data) {
        //calculate total process from data
        total_process[var] += data;
        ++data;
    }

    //close file
    inFile.close();

    //print total process
    cout << " Sum = " << total_process[var] << endl;         
}

return 0;

}

The code was run as it supposed. But the problem occur after performing total process calculation. Example of output :

It give some improper value

Sorry if the code was not good in design. I'm still new in programming.

There are some issues.

1) Arrays in C++ are indexed from 0, not from 1. That means that when you have an array of 30 elements, allowed indices are from 0 to 29. But in your loop var iterates from 1 to 30, so the last iteration tries to use total_process[30] when the last 'truly' accessible element is total_process[29] . Such errors can be very difficult to debug, as when you write an element out of bounds of your array, you corrupt surrounding memory, so you can change some other variable in that way. To fix this, either iterate with for (var = 0; var < limit; var++) , or use var - 1 index like this: total_process[var - 1] .

2) Variables and arrays of primitive types such as int are left uninitialized by default, and you shouldn't access such uninitialized variables. Always make sure that when you use some variable it already has some assigned value. You can initialize your array with zeros like int arr[30] = {0}; , int arr[30] = {} or int arr[30]{} .

Also be careful and don't get confused with the first way to initialize an array:

int arr[30] = {1};

This doesn't initialize all the elements with 1, but just initializes arr[0] with 1 and all the other elements with 0. It works so because you can do this:

int arr[30] = {1, 2, 3, 4, 5}; // all other will be 0

I think you never initialized your result array total_process .

So you should zero-initialize it with

 int total_process[30] = { 0 }; 

When a program is not behaving like you think it should, you should be using a debugger. That way you can step through the execution and see where it has values or calculations that you didn't expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM