简体   繁体   English

Fscanf没有从文件的最后一行读取预期值(C / C ++)

[英]Fscanf is not reading expected values from last line of file (C/C++)

The code below is file reading function for my task scheduling program. 下面的代码是我的任务计划程序的文件读取功能。 Everything is OK, until it tries to read sequence of numbers (ex 1, 2, 3, 4, 5) representing deadlines. 一切正常,直到尝试读取代表截止日期的数字序列(例如1、2、3、4、5)。 Instead of ints some random numbers are added to the table "Deadlines". 而不是整数,一些随机数被添加到表“期限”中。 Does anyone know solution for this problem? 有谁知道解决这个问题的方法吗?

    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <fstream>
    #include <map>
    #include <time.h>
    using namespace std;
    class Task{
        public:
            string Name;
            int Number;
            int Operation_Time_Machine_One;
            int Operation_Time_Machine_Two;
            int Task_Deadline;
    };
    int Maximum_Time_Between_IDE;
    int IDE_Duration;
    int Read_File(){
        string FileName= "";
        cout << "Please enter file name (with .txt):\t";
        cin >> FileName;
        std::vector<Task> Task_Vector;
        FILE* OpenedFile = fopen(FileName.c_str(),"r");
        int Number_Of_Tasks=0;
        int Duration_Of_Operation_One =0;
        int Duration_Of_Operation_Two =0;
        if( OpenedFile == NULL){
            exit(EXIT_FAILURE);
        }
        else{
                fscanf(OpenedFile,"%d", &Number_Of_Tasks);
                for (int i=0; i<= Number_Of_Tasks;i++){
                    Duration_Of_Operation_One=0;
                    Duration_Of_Operation_Two=0;
                    fscanf(OpenedFile, "%d, %d", &Duration_Of_Operation_One, &Duration_Of_Operation_Two);
                    Task New_task;
                    New_task.Name ="task" ;
                    New_task.Number = (i+1);
                    New_task.Operation_Time_Machine_One = Duration_Of_Operation_One;
                    New_task.Operation_Time_Machine_Two = Duration_Of_Operation_Two;
                    New_task.Task_Deadline =0;
                    Task_Vector.push_back(New_task);
                }
                fscanf(OpenedFile,"%d", &Maximum_Time_Between_IDE);
                fscanf(OpenedFile,"%d", &IDE_Duration);
                int Deadlines[Number_Of_Tasks];
//from now it's gettig weird
               for(int i=0; i<=Number_Of_Tasks;i++){
                    fscanf(OpenedFile,"%d, ", &Deadlines[i]);
                }
                for(int i=0; i<=Number_Of_Tasks;i++){
                    cout<<Deadlines[i]<<endl;
                }
        }
        fclose(OpenedFile);

        return 0;
    }
    int main()
    {
        clock_t time;
        time = clock();
        std::cout << "Hello world!" << std::endl;
        Read_File();
        time = clock() - time;
        printf ("Time of algorithm %f seconds.\n",((float)time)/CLOCKS_PER_SEC);
        return 0;
    }

Sample input: 输入样例:

5
74, 64
27, 74
69, 47
35, 54
101, 86
102
6
113, 242, 344, 144, 513

There are some errors.. 有一些错误..

int Deadlines[Number_Of_Tasks]

VLA's are not allowed in C++. C ++中不允许使用VLA。 Therefore you should remove the C++ tag, or use std::vector everywhere you want a VLA. 因此,您应该删除C++标记,或在需要VLA的任何地方使用std::vector ie std::vector<int> Deadlines(Number_Of_Tasks); std::vector<int> Deadlines(Number_Of_Tasks);

for(int i=0; i <= Number_Of_Tasks; i++)

  .... Deadlines[i] 

you're exceeding the bounds of the array, which leads to undefined behavior. 您超出了数组的界限,这将导致未定义的行为。 What you want to do is (in both the last two loops): 您想要做的是(在最后两个循环中):

for(int i=0; i < Number_Of_Tasks; i++)
              ^^^

Finally, don't combine C and C++ which are two different programming languages. 最后,不要结合使用C和C ++这两种不同的编程语言。 Choose one of them for your program and stick to it. 为您的程序选择其中之一并坚持下去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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