使用流读取文件
我想从一个字节到另一个字节读取文件。但我的程序在第一个 while
处就中断了,
ifstream infile("new.pdf",ifstream::binary);
ofstream outfile("file.pdf",ofstream::binary);
char *c;
while(infile.read(c,sizeof(char)))
{
mpz_t M;
mpz_t K;
mpz_init2(M,10);
mpz_set_ui(M,(int)c);
mpz_init2(K,10);
mpz_powm(K,M,e,n);
char *x;
mpz_get_str(x,10,K);
outfile.write(x,sizeof(char));
}
infile.close();
outfile.close();
system("pause");
infile.open("file.pdf",ifstream::binary);
outfile.open("newt.pdf",ofstream::binary);
while(infile.read(c,sizeof(char)))
{
mpz_t C;
mpz_t K;
mpz_init2(K,10);
mpz_init2(C,10);
int x=(int)c;
mpz_set_ui(K,x);
mpz_powm(C,K,d,n);
char *s;
mpz_get_str(s,10,C);
outfile.write(s,sizeof(char));
}
infile.close();
outfile.close();
这是什么问题?
我需要 ecnrypt 文件然后对其进行加密。 所以我决定从输入文件中读取字节,对其进行加密,然后写入另一个文件。
I want to read file from byte to byte. But my program breaks on the first while
ifstream infile("new.pdf",ifstream::binary);
ofstream outfile("file.pdf",ofstream::binary);
char *c;
while(infile.read(c,sizeof(char)))
{
mpz_t M;
mpz_t K;
mpz_init2(M,10);
mpz_set_ui(M,(int)c);
mpz_init2(K,10);
mpz_powm(K,M,e,n);
char *x;
mpz_get_str(x,10,K);
outfile.write(x,sizeof(char));
}
infile.close();
outfile.close();
system("pause");
infile.open("file.pdf",ifstream::binary);
outfile.open("newt.pdf",ofstream::binary);
while(infile.read(c,sizeof(char)))
{
mpz_t C;
mpz_t K;
mpz_init2(K,10);
mpz_init2(C,10);
int x=(int)c;
mpz_set_ui(K,x);
mpz_powm(C,K,d,n);
char *s;
mpz_get_str(s,10,C);
outfile.write(s,sizeof(char));
}
infile.close();
outfile.close();
What's the problem?
I need to ecnrypt file and then encrypt it.
So I decided to read byte from input file, ecnrypt it and the write to another file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将未初始化的指针传递给读取函数,这是未定义的行为。
试试这个:
You are passing an uninitialized pointer into the read function, this is undefined behavior.
Try this:
它返回流,因此在第一个 while 之前使用 ifstream::good() 检查流是否良好。此外,您可能需要使用相同的函数来检查文件结尾。
it returns the stream, so before the first while check if the stream is good by using ifstream::good() . Also you might need to use the same function to check end of file.