c++ 文件输出问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 18:56:53
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <map>

using namespace std;
const string OUTPUT_FILE_NAME="2.dat";
map<string,string>imap;//储存图片相关信息

bool InitMap()//读入图片相关信息到map中
{
fstream file;
file.open("d:\\1.dat" , ios::in); //以读的方式打开文件
if(!file)
{
return false;
}
string line;
string key;
string remark;
while(getline(file,line))
{
stringstream stream(line);
stream>>key;
remark = stream.str();

imap.insert(make_pair(key , remark));
}

file.close();
}

//将图片名与map中比较
void Compare(string name)
{
ofstream outputFile(OUTPUT_FILE_NAME.c_str());
map <string , string >::iterat

原因是打开输出文件的方法存在问题。比如:指令
ofstream outputFile(OUTPUT_FILE_NAME.c_str());
打开输出文件,缺省将清空文件内容。建议修改为
ofstream outputFile(OUTPUT_FILE_NAME.c_str(), ios::out|ios::app);