Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

c++ - 从文件.txt读取数据(Read data from file .txt)

I have a c++ code here.

(我在这里有一个C ++代码。)

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 :
";

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.

(我仍然是编程新手。)

  ask by azrin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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] .

(1)C ++中的数组从0开始索引,而不是从1开始索引。这意味着当您有30个元素的数组时,允许的索引从0到29。但是在循环变量中, var从1到30进行迭代,因此最后一次迭代尝试当最后一个“真正”可访问元素是total_process[29]时,使用total_process[30] 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] .

(要解决此问题,请使用for (var = 0; var < limit; var++)进行迭代,或使用var - 1索引,例如: 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.

(2)默认情况下,原始类型(例如int )的变量和数组未初始化,并且您不应该访问此类未初始化的变量。)

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] = {0};)

, int arr[30] = {} or int arr[30]{} .

(, int arr[30] = {}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:

(这不会用1初始化所有元素,而只是用1初始化arr[0]以及用arr[0]初始化所有其他元素。之所以起作用是因为您可以这样做:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...