C++ 施工人员未按预期工作

C++ 施工人员未按预期工作,c++,C++,我正试图让这个构造器执行以下操作: 此构造函数尝试打开传递名称的文件 在文件名中删除它。若文件打开成功,则调用函数 getFileSize以确定应分配的字节数 为了这个消息。为消息分配空间并读取 将文件中的内容放入其中。在结尾处关闭文件。 成员变量长度应设置为文件大小。 如果找不到文件,则应将长度设置为零 我目前遇到了问题,当我试图运行我的程序时,我得到了一个错误,因为它甚至没有读取我的文件,并且无法理解问题。任何帮助都将不胜感激 建造商: Message::Message(std::strin

我正试图让这个构造器执行以下操作:

此构造函数尝试打开传递名称的文件 在文件名中删除它。若文件打开成功,则调用函数 getFileSize以确定应分配的字节数 为了这个消息。为消息分配空间并读取 将文件中的内容放入其中。在结尾处关闭文件。 成员变量长度应设置为文件大小。 如果找不到文件,则应将长度设置为零

我目前遇到了问题,当我试图运行我的程序时,我得到了一个错误,因为它甚至没有读取我的文件,并且无法理解问题。任何帮助都将不胜感激

建造商:

Message::Message(std::string filename) {
    fstream fin(filename);
    if (fin.fail())
    {
        cout << "failed";
    }
    else {
        length = getFileSize(fin);
        message = new char[length];
        fin.getline(message, length); {
            fin >> message;
        }
    }
    fin.close();
}
问题

    message = new char[length];
    fin.getline (message, length); {
        fin >> message;
    }
  • 如果遇到换行符,
    getline
    将停止
  • fin>>消息行将覆盖在
    getline
    中读取的内容
  • {
    }
    毫无意义。这些问题本身并不是问题,但它们让我觉得你不清楚自己想做什么
  • 我会把那些台词改成

        message = new char[length + 1];  // Add an extra character if
                                         // message is supposed to be null
                                         // terminated.
        fine.read(message, length);
        message[length] = '\0';
    

    请尝试创建并在此处发布代码。@RSahu好的,我尝试在线程中添加可以重现问题的最重要部分,好的,所以我做了这些,但现在我得到的只是胡言乱语,它们似乎不是整个文档的长度。因为我知道至少有100多个字符,所以我只得到了5-10个字符的垃圾。@MaxMillin,问题可能与此有关。我还在阅读它,所以基本上我必须编写我的析构函数,这可能会一起导致问题?@MaxMillin更糟,如果无法在构造函数中打开文件,则将整个成员变量集保持为未初始化状态,并且创建
    消息
    的机制不可能提供任何实际发生的线索。强化您的代码,并阅读提供的关于三法则编程的链接。然后,通过使用
    std::vector
    等切换到RAII习惯用法,并庆幸三个规则不再是问题。在文件无法打开并成功处理的情况下,这将非常重要。
    消息的所有者无法知道这一点。此外,如果您正确地编写了析构函数,并且它是成员
    消息
    ,您如何确切地知道要删除的内容<代码>消息
    可以是任何内容,包括有效指针,因为文件处理工作正常;也可以是不确定的,因为文件处理不工作。关键是,除非你知道你的数据所持有的价值是有信誉的,否则根据所述价值采取行动是错误的。
    /*
    * Message.h
    *
    *  Created on: Dec 11, 2016
    *      Author: hellenpacheco
    */
    
    #ifndef MESSAGE_H_
    #define MESSAGE_H_
    
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    
    class Message
    {
    private:
        char *message;   // holds the message
        int length;  // holds the the message length
        static const short ALPHABET_SIZE = 26;
        char code[ALPHABET_SIZE]; // holds the cypher alphabet
                                  // iztohndbeqrkglmacsvwfuypjx
                                  // ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
    
                                  // returns the input file size in bytes
        std::streamsize getFileSize(std::fstream &file) const
        {
            std::streamsize fsize = 0;
            file.seekg(0, std::ios::end);
            fsize = file.tellg();
            file.seekg(0, std::ios::beg); // moves file pointer back to the beginning
            return fsize;
        }
    public:
        /*
        * This constructor tries to open the file whose name is passed
        * to it in filename. If file opens successfully, calls function
        * getFileSize to determine how many bytes should be allocated
        * for the message. Allocates space for message and reads the
        * content from the file into it. Closes the file at the end.
        * Member variable length should be set to the file size.
        * If file cannot be found, length should be set to zero.
        */
        Message(std::string filename);
    
        // The destructor frees the space allocated to message
        virtual ~Message();
    
        // Decodes the message
        void decode();
    
        // Capitalizes first letter in each sentence
        void fixCapitalization();
    
        // Prints the content of message on the screen
        void dump() const;
    
        // Returns true if the message is empty
        bool isEmpty() const;
    };
    
    
    
    #endif /* MESSAGE_H_ */
    
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    #include "Message.h"
    
    using namespace std;
    
    int main()
    {
        // create a message object with the content of Encrypted.txt
        Message m("Encrypted.txt");
    
        if (m.isEmpty())
        {
            cout << "Could not read message";
            return EXIT_FAILURE;
        }
        cout << "Original message: " << std::endl;
        m.dump();
        cout << std::endl << std::endl;
        m.decode();
        m.fixCapitalization();
        cout << "Decoded message: " << std::endl;
        m.dump();
        cout << std::endl << std::endl;
    
        return EXIT_SUCCESS;
    }
    
    ifqkwxcadf ar cei fpoi masif cd cei xkdqirr du pxxnwafm pf pnmdkaceo cd p oirrpmi, teaqe rqkpohnir cei gpcp af ac-oplafm ac sikw gauuaqvnc pfg caoi qdfrvoafm, au fdc xkpqcaqpnnw aoxdrrahni, cd gigvqi cei dkamafpn masif dfnw cei ifqdgig gpcp. afxvcr cd cei pnmdkaceo cwxaqpnnw afsdnsi pggacadfpn riqkic gpcp qpnnig liwr, teaqe xkisifcr cei oirrpmi ukdo hiafm giqdgig-isif au cei pnmdkaceo ar xvhnaqnw lfdtf.
    
        message = new char[length];
        fin.getline (message, length); {
            fin >> message;
        }
    
        message = new char[length + 1];  // Add an extra character if
                                         // message is supposed to be null
                                         // terminated.
        fine.read(message, length);
        message[length] = '\0';