【发布时间】:2012-01-18 16:09:54
【问题描述】:
我正在制作一个移位密码,它从文件中读取文本并对其进行解码。解密工作正常但是我无法弄清楚如何找到文件的长度而不将其硬编码为 char 数组的大小。它也只读取一行,任何带有换行符的内容都会损坏。
任何帮助将不胜感激,我省略了主要代码块,因为它在读入数组后处理数组,看起来有点长且无关紧要。
string fileName;
cout << "Please enter the locations of your encrypted text (e.g ""encryptedText.txt""): ";
getline( cin, fileName );
char encryptedMessage[446]; //How do i read in the file length and declare the array size as a variable instead of [446]?
char decryptedMessage[446];
ifstream in(fileName);
if(in.get(encryptedMessage, 446))
{
[my decrypting code]
}
else
{
cout << "Couldn't successfully read file.\n";
}
system("pause");
【问题讨论】:
-
查看previous answer 中的第四个测试用例。将其
s4从string更改为vector,它应该已经准备就绪了。
标签: c++ string file-io fstream