关于VC++从文件中按组读取的问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 22:10:44
就是从一个TXT文件中按每64位读取然后加密,读完所有内容后,不足64位的最后一组用0补完,最终读入另一个文件中,请问怎么实现?
我的只能读出前64位:
unsigned char plaintext[64] = {0};
unsigned char ciphertext[64] = {0};
char infile[10];
if (argc != 2)
{
cout << "Please enter a plaintext filename." << flush;
cout << endl;
cin >> infile;
}
else
{
strcpy(infile, argv[1]);
}

ifstream input(infile, ios::in);
if (input == 0)
{
cerr << "Couldn't open the plaintext file " << infile << endl;
exit(1);
}
for (int i = 0; i < 64; i++)
{
plaintext[i] = input.get();
}
cout << "Please enter a filename to output the ciphertext." << flush << endl;

char outfile[10];
cin >> outfile;
ofstream output(outfile, ios::out);
if (!outfile)
{
cerr << "openg error!&

while(没有到文件尾)
{
if(读成功)
加入数组
else
加零
}

建议你用CStdioFile类来实现,
CStdioFile cFile;
略过文件打开语句。

定义两个ULONGLONG变量
ULONGLONG uLen,uRemain;
用其GetLength函数获得整个文件的长度(字节数)uLen(ULONG变量),
uLen=cFile.GetLength();

uRemain=ulen%64; // 求文件长度除64的余数
建立自己的处理数组 char plaintext[uLen+uRemain];
将cFile文件内容读入plaintext
cFile.Read(plaintext, uLen); // 这一句可以直接读完整个文件
for (BYTE i=uLen;i<uRemain;i++)
plaintext[i]=0; // 后面补上uRemain个零

如果你喜欢一个个字节的读取文件的话,
判断一下读到的字符是否等于"EOF"就知道是否到达文件结尾了。
好好看看MSDN关于CFile类成员的说明吧,
virtual ULONGLONG Seek(
LONGLONG lOff,
UINT nFrom
);
此函数可以定位到文件的从nFrom偏移lOff字节的位置。nFrom可选参数为CFile::begin、CFile::current、CFile::end。
CFile类的SeekToBegin和SeekToEnd函数可以直接跳到头或者尾。