c++ - Fscanf 未从文件的最后一行读取预期值 (C/C++)

标签 c++ c c++11 scanf

下面的代码是我的任务调度程序的文件读取功能。一切正常,直到它尝试读取代表截止日期的数字序列(例如 1、2、3、4、5)。而不是整数,一些随机数被添加到表“截止日期”中。有谁知道这个问题的解决方案吗?

    #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;
    }

示例输入:

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

最佳答案

有些错误..

int Deadlines[Number_Of_Tasks]

VLA 在 C++ 中是不允许的。因此,您应该删除 C++标记,或使用 std::vector任何你想要 VLA 的地方。即std::vector<int> Deadlines(Number_Of_Tasks);

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

 .... Deadlines[i]

您超出了数组的边界,这会导致未定义的行为。你想要做的是(在最后两个循环中):

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

最后,不要将 C 和 C++ 这两种不同的编程语言混为一谈。为您的计划选择其中一项并坚持下去。

关于c++ - Fscanf 未从文件的最后一行读取预期值 (C/C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41916440/

相关文章:

c++ - 如何从可变模板参数的嵌套类派生?

c - 在 C 中从 typedef 结构设置和获取数组的值

c - Lua C API : Initializing a variable matrix in a structure C

C++:未计算上下文中的 lambda 表达式

C++:没有用于调用 std::deque<SnakePart>::emplace_front 的匹配函数

c++ - 有没有更有效的方法来执行此算法?

c++ - TinyXml 解析器拒绝正确加载文件

c++ - C 如何测量 <time.h> 中的时间

c - 不同数据类型的大小导致混淆

c++ - 寻找滥用枚举的替代方法