缓冲区溢出的问题

通天神树 2013-09-30 04:15:37
头文件
#ifndef CIPHER_H
#define CIPHER_H

class Cipher {
public:
Cipher();
virtual ~Cipher();
char encode(char );
void print();

private:
char alphabet[27];
char cipherkey[27];
void shuffle( char[] , int );
};

#endif



#include <iostream>
#include <time.h>
#include <string.h>
#include "Cipher.hpp"

using namespace std;

Cipher::Cipher() {

strcpy( alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
strcpy( cipherkey, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );

shuffle( cipherkey, 26 );
}

Cipher::~Cipher() {
}

void Cipher::shuffle( char charArray[] , const int size ) {
static_cast<unsigned int>(time(NULL));
for(int k=size;k>1;k--)
{
char temp,randpos;
randpos=rand()%k;
temp=charArray[k-1];
charArray[k-1]=charArray[randpos];
charArray[randpos]=temp;
}

return;
}

char Cipher::encode( char ch ) {
int n=ch;
if (n>=97&&n<=122)
n=n-32-65;
else if(n>=65&&n<=90)
n=n-65;
else
return ch;
return ch=cipherkey[n];
}

void Cipher::print() {
cout<<"alphabet:"<<alphabet<<endl;
cout<<"cipherkey:"<<cipherkey<<endl;
}



主程序
#include "tools.hpp"
#include "Cipher.hpp"
#include <iostream>
#include <fstream>

using namespace std;

int main( void ) {

banner();

Cipher c;


c.print();

ifstream infile("F:\\Fall 2013\\CS 620\\P1.txt");
ofstream outfile("F:\\Fall 2013\\CS 620\\P1.enc");

if(!infile){
fatal("%s\n", "Unable to open infile");
}

if(!outfile){
fatal("%s\n", "Unable to open outfile");
}

char e;
infile.open("F:\\Fall 2013\\CS 620\\P1.txt",ios::in);
while (infile.peek()!=EOF){
infile>>e;
e=c.encode(e);
outfile<<e;
}
infile.close();
outfile.close();

bye();

return 0;
}


麻烦帮忙啊谢谢
...全文
141 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
通天神树 2013-09-30
  • 打赏
  • 举报
回复
引用 3 楼 LanXLawlieT 的回复:
虽然没彻底看懂你的代码的意思,但是蜗居的 for(int k=size;k>1;k--) 这个循环是有问题的,你的字母数是26个,但是循环从26~2,这里你应该好好检查一下是不是存储的长度出问题了,或者是数组的下标越界了。
现在缓冲区的问题没了,但是有一个新问题了 我P1.txt里的东西读出来处理过后输出到P1.enc里,不能处理换行 也就是说我在txt中有好几行,但是到enc里就只有一行了 另外再enc的末尾还会比txt多一个字符,怎么搞得啊
LanXLawlieT 2013-09-30
  • 打赏
  • 举报
回复
虽然没彻底看懂你的代码的意思,但是蜗居的 for(int k=size;k>1;k--) 这个循环是有问题的,你的字母数是26个,但是循环从26~2,这里你应该好好检查一下是不是存储的长度出问题了,或者是数组的下标越界了。
通天神树 2013-09-30
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
有问题请描述清楚,方便他人理解。
哦,就是调试的时候弹提示“在已损坏了程序内部状态的 P1.exe 中发生了缓冲区溢出。按“中断”以调试程序,或按“继续”以终止程序。” 不知道哪里溢出了以及怎么解决啊
qq120848369 2013-09-30
  • 打赏
  • 举报
回复
有问题请描述清楚,方便他人理解。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧