如何传递这个字符串C ++

时间:2015-12-06 23:34:44

标签: c++

所以我得到了这个任务,除了调用这个应该格式化电子邮件字符串的函数之外,我完成了所有工作。如果不调用此功能,我可以按照教授所说的那样打印它。但是,通过调用此函数,我似乎无法让它返回信息并打印出来。任何人都可以帮我解决这个问题吗?我花了不少时间试图解决这个问题。

//Function Prototypes
std::string FormatEmailString(std::string from, std::string to, std::string subject, std::string msg);
std::string GetInboxFile(std::string username);
std::string GetLine(std::istream & sin);


int main()
{
    std::string email;
    std::string from;
    std::string to;
    std::string subject;
    std::string msg;

    std::ifstream fin;
    std::ofstream fout;
    std::string user1;

    fin.open(user1 + "-inbox.txt", std::ios::in);

    while (fin.eof() == false) // I know fin.eof is not a good method to use.  
    {                          // I tried while(fin) but it lets extra "From:" "To"...

        email = GetLine(fin); //GetLine is a function call to getline.
        from = GetLine(fin);
        to = GetLine(fin);
        subject = GetLine(fin);
        msg = GetLine(fin);

        std::string formatted = FormatEmailString(from, to, subject, msg);
        fout << formatted;

    }
    std::cout << std::endl;
    fin.close();
    std::cin.get();
    return 0;
}

std::string FormatEmailString(std::string from, std::string to, std::string subject, std::string msg)
{

    std::ostringstream out;
    std::ifstream fin;

    std::string user1;
    fin.open(user1 + "-inbox.txt", std::ios::in);

    out << std::endl;
    out << "From: " << from << std::endl;
    out << "To: " << to << std::endl;
    out << "Subject: " << subject << std::endl;
    out << "Message: " << msg << std::endl;

    fin.close();
    return out.str();

}//END FormatEmailString

std::string GetLine(std::istream & sin)
{

    std::string s;
    std::getline(sin, s);
    return s;

}//END GetLine

//FROM user1-inbox.txt TEXT FILE:
 #email             //This Line not printed
 abc                //FROM
 user1              //TO
 hello              //Subject
 How about lunch?   //Message
 #email             //This line not printed
 abc                //From
 user1              //To
 Join the Dark Side //Subject
 We have cookies!   //Message
 //END TEXT FILE

1 个答案:

答案 0 :(得分:0)

您没有打开要写入的文件。 你的fout仅在两个地方使用......在声明中,然后你正在写入它。