构造函数未按预期工作
Constructor not working as intended
我试图让这个构造函数做这些事情:
此构造函数尝试打开传递其名称的文件 在文件名中。如果文件成功打开,调用函数 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();
}
.h 文件:
#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:
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;
};
这是我的文件:
对象。.CPP:
#include "Message.h"
using namespace std;
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::~Message()
{
//dtor
}
void Message::decode() {
int offset;
strcpy(code, "iztohndbeqrkglmacsvwfuypjx");
for (int i = 0; i < strlen(message); i++) {
if (message[i] == ' ') continue;
if (message[i] == ',') continue;
if (message[i] == '.') continue;
offset = int(message[i] - 'a');
message[i] = code[offset];
}
}
void Message::fixCapitalization() {
for (int i = 0; i < strlen(message); i++) {
if (message[0] != ' ' || message[0] != ',') {
message[0] = toupper(message[0]);
}
if (message[i] == '.' || message[i] == '?' || message[i] == ',') {
message[i + 2] = toupper(message[i + 2]);
}
}
}
void Message::dump() const {
for (int i = 0; i < strlen(message); i++) {
cout << message[i];
}
}
bool Message::isEmpty() const {
if (length == 0) {
return true;
}
else {
return false;
}
}
.H 文件:
/*
* 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_ */
主要。.CPP:
#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;
}
以下文件是我尝试打开和"解码".txt文件,全部在 1 行上:
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;
}
- 如果遇到换行符,
getline
将停止。 fin >> message;
行将覆盖getline
中读取的内容。{
和}
根本没有意义。它们本身不是问题,但它们使我认为你不清楚你想做什么。
我会将这些行更改为
message = new char[length + 1]; // Add an extra character if
// message is supposed to be null
// terminated.
fine.read(message, length);
message[length] = ' ';
相关文章:
- 优先级队列构造函数的工作
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 为什么我的类工作正常,即使在返回垃圾值作为赋值运算符和空复制构造函数的返回之后
- 成员的初始值(如"content(ht * wd, c)"C++ Primer 的构造函数中如何工作?
- 隐式生成的移动构造函数在 c++ 中如何工作?
- "= 删除"如何工作?有人可以解释这个构造函数吗?
- 如何让 call_once() 在构造函数中正常工作?
- C++参数化构造函数使代码在传递大输入时停止工作
- 我是否应该在构造函数或成员方法中进行大量初始化工作
- 在 GCC 中工作的外行构造函数模板在 Clang 中失败
- 为什么带有 const 关键字的构造函数可以工作,而没有它就不能工作?
- 共享_ptr复制构造函数如何工作
- VS为我提供了对构造函数的另一个解决方案,但我想知道为什么我的工作不起作用
- 当默认构造函数充当默认参数时,它是如何工作的
- 构造函数和析构函数的工作原理
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- 隐式删除的默认构造函数在 C++14 中工作
- 构造函数未按预期工作
- 以 std::p air 作为参数的构造函数:T a({1,2}) 有效,T a = {1,2} 不工作
- 为什么我的序列构造函数无法正常工作