【发布时间】:2014-04-19 01:38:11
【问题描述】:
我的代码加载了一个加密的文本文件,这个文本文件的第一行是一个密码,用于解码文本文件的其余部分。我知道如果函数未在 main 之前定义,通常会发生此错误,但我已在标头中定义了它,所以我不确定是什么导致了此问题。
#include <string>
#include <iostream>
#include <fstream>
std::string decrypt(std::string cipher, std::string text);
int main()
{
//variable
std::string fileName;
std::string cipher;
std::string text;
//prompt the user to enter the file name
std::cout << "Enter file name: ";
std::cin >> fileName;
//declare input stream
std::ifstream inFile(fileName);
//Checks if the file has been connected or not
if (inFile.is_open())
{
std::cout << "File found." << std::endl;
std::cout << "Processing..." << std::endl;
getline(inFile,cipher); //get the first line as one string
std::cout << cipher << std::endl; //TEST!!!!
std::cout << "\n" << std::endl; //TEST!!!!!!
while (inFile.good()) //continue running until an error occurs to get the rest of the text as second string
{
getline(inFile,text); //get the first line as one string
std::cout << text << std::endl; //TEST!!!!
}
}
else //if the file isn't connected
{
std::cout << "File not found." << std::endl; **(ERROR C3861 HERE)**
}
inFile.close(); //close the file
std::string finalResult = decrypt(cipher,text);
std:: cout << finalResult << std::endl;
return 0;
}
//Decrypt function: Takes 2 strings by reference, changes the second string into the unencrypted text
std::string decrypt(std::string cipher, std::string text)
{
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //create a character array of the alphabet
char code[27]; //empty array
char oldText[500]; //empty array
strcpy(code,cipher.c_str()); //copies the cipher into the array
strcpy(oldText,text.c_str()); //copies the encrypted text into the array
for (int i = 0; i < 500;i++) //Loop through the entire encrypted text
{
int k = i; //creates and resets a variable for while loop
while (alphabet[k] != oldText[i])
{
k = k+1;
}
oldText[i] = alphabet[k]; //after a match is detected, swap the letters
}
std::string newText(oldText); // converts the array back into text
return newText;
}
【问题讨论】:
标签: c++ visual-studio compiler-errors runtime-error