关于下面程序的运行问题

tmwanly 2013-03-22 04:16:41

#include "targetver.h"
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <string>
#include<fstream>//文件输入输出的流类
#include <sstream>


using namespace std;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int _tmain(int argc, _TCHAR* argv[])
{
map<string,string> trans_map;
string key,value;
if (argc!=3)
{
throw runtime_error("wrong number of arguments");//debug error
}
ifstream map_file;
if (!open_file(map_file,argv[1])) //debug assertion failed
{
throw runtime_error("no transformation file");
}
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
if (!open_file(input,argv[2]))
{
throw runtime_error("no input file");
}
string line;
while(getline(input,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if (map_it!=trans_map.end())
{
word=map_it->second;
}
if (firstword)
{
firstword=false;
}
else
cout<<" ";
cout<<word;
}
cout<<endl;
}

return 0;
}


通过编译了但是不能正常运行
...全文
388 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
2826635869 2014-05-09
  • 打赏
  • 举报
回复
引用 21 楼 tmwanly 的回复:
[quote=引用 20 楼 dy106 的回复:] 引用 19 楼 tmwanly 的回复:引用 18 楼 dy106 的回复:楼主考虑重新建立一个工程 Win32 Console Application 我这个就是 Win32 Console Application啊 好郁闷啊 从建效果也一样用你运行好的代码贴上去调试也是0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位……
终于知道为什么了,忘记加后面了.txt了 太谢谢了[/quote] 哪里加.txt呢 请赐教啊
wchar_t 2013-03-23
  • 打赏
  • 举报
回复
bool open_file(ifstream &in,const string &file) { in.close(); in.clear(); in.open(file.c_str()); return in.is_open(); } 这么写
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
引用 23 楼 tmwanly 的回复:
引用 22 楼 dy106 的回复: 用这种方式应该怎么打开呢?
楼主仔细学学怎么给main函数传参,只要参数传对了,你的方法是对的
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 22 楼 dy106 的回复:
用这种方式应该怎么打开呢?
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 20 楼 dy106 的回复:
引用 19 楼 tmwanly 的回复:引用 18 楼 dy106 的回复:楼主考虑重新建立一个工程 Win32 Console Application 我这个就是 Win32 Console Application啊 好郁闷啊 从建效果也一样用你运行好的代码贴上去调试也是0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位……
终于知道为什么了,忘记加后面了.txt了 太谢谢了
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
引用 19 楼 tmwanly 的回复:
引用 18 楼 dy106 的回复:楼主考虑重新建立一个工程 Win32 Console Application 我这个就是 Win32 Console Application啊 好郁闷啊 从建效果也一样用你运行好的代码贴上去调试也是0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0031f6c8 处的 std:……
重新建立一个 在最后一步的时候不要选Precompiled header,选第一个empty.... 然后自己添加cpp文件 试试
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 18 楼 dy106 的回复:
楼主考虑重新建立一个工程 Win32 Console Application
我这个就是 Win32 Console Application啊 好郁闷啊 从建效果也一样用你运行好的代码贴上去调试也是0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0031f6c8 处的 std::runtime_error
if (!open_file(map_file,file))  //debug assertion failed
    {
        throw runtime_error("no transformation file");//在这一句
    }
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
楼主考虑重新建立一个工程 Win32 Console Application
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 16 楼 dy106 的回复:
引用 15 楼 tmwanly 的回复:C/C++ code?1234if (!open_file(map_file,file)) //debug assertion failed { throw runtime_error("no transformation file"); }
这一句就卡了 0x7607812f 处未处理的异常: Mic……


你用的什么编译器啊,这个是不是和编译器设置有关系呢
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
引用 15 楼 tmwanly 的回复:
C/C++ code?1234if (!open_file(map_file,file)) //debug assertion failed { throw runtime_error("no transformation file"); } 这一句就卡了 0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位置 ……
楼主确定是把你要打开的文件跟cpp文件放在一个目录下吗?
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
if (!open_file(map_file,file))  //debug assertion failed
    {
        throw runtime_error("no transformation file");
    }
这一句就卡了 0x7607812f 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0030f434 处的 std::runtime_error。 'en them cuz because gratz grateful i r nah no pos supposed sez said tanx thanks wuz was 文本文件里面的内容 通过前面的找到后面的如 输入sez出来的是said
_sunshine 2013-03-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <string>
#include<fstream>//文件输入输出的流类
#include <sstream>

using namespace std;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main()
{
map<string,string> trans_map;
string key,value;
/*if (argc!=3)
{
throw runtime_error("wrong number of arguments");//debug error
}*/
string file = "1.txt"; //文件里面存:
//Chinese 中文
//English 英文
//Japanese 日文
ifstream map_file;
if (!open_file(map_file,file)) //debug assertion failed
{
throw runtime_error("no transformation file");
}
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
string fil = "2.txt"; //文件里面存:
//Chinese
//English
//Japanese
if (!open_file(input,fil))
{
throw runtime_error("no input file");
}
string line;
while(getline(input,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if (map_it!=trans_map.end())
{
word=map_it->second;
}
if (firstword)
{
firstword=false;
}
else
cout<<" ";
cout<<word;
}
cout<<endl;
}

system("pause");
return 0;
}


结果:
_sunshine 2013-03-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <string>
#include<fstream>//文件输入输出的流类
#include <sstream>

using namespace std;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main()
{
map<string,string> trans_map;
string key,value;
/*if (argc!=3)
{
throw runtime_error("wrong number of arguments");//debug error
}*/
string file = "1.txt"; //文件里面存Chinese 中文
English 英文
Japanese 日文
ifstream map_file;
if (!open_file(map_file,file)) //debug assertion failed
{
throw runtime_error("no transformation file");
}
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
string fil = "2.txt"; //文件名
if (!open_file(input,fil))
{
throw runtime_error("no input file");
}
string line;
while(getline(input,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if (map_it!=trans_map.end())
{
word=map_it->second;
}
if (firstword)
{
firstword=false;
}
else
cout<<" ";
cout<<word;
}
cout<<endl;
}

system("pause");
return 0;
}
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 10 楼 dy106 的回复:
引用 9 楼 dy106 的回复:C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include <iostream>#include <stdio.h>#……
你刚改的我试了 不行啊
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 8 楼 tofu_ 的回复:
这个是做啥?词典啊? 我测试时,第一个文件内容: Chinese 中文 English 英文 Japanese 日文 第二个文件内容: Japanese English Chinese 完美输出: 日文 英文 中文
··· 嗯
_sunshine 2013-03-22
  • 打赏
  • 举报
回复
引用 9 楼 dy106 的回复:
C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include <iostream>#include <stdio.h>#include <tchar.h>……
楼主在传参给main的时候出了问题
_sunshine 2013-03-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <string>
#include<fstream>//文件输入输出的流类
#include <sstream>

using namespace std;
ifstream& open_file(ifstream &in,const string &file)
{
	   in.close();
	   in.clear();
	   in.open(file.c_str());
	   return in;
}
int main()
{
	map<string,string> trans_map;
	string key,value;
	/*if (argc!=3)
	{
		throw runtime_error("wrong number of arguments");//debug  error
	}*/
	string file = "xxxxxx"; //文件名
 	ifstream map_file;
	if (!open_file(map_file,file))  //debug assertion failed
	{
		throw runtime_error("no transformation file");
	}
	while(map_file>>key>>value)
		trans_map.insert(make_pair(key,value));
 	ifstream input;
	string fil = "xxxxxxx"; //文件名
	if (!open_file(input,fil))
	{
		throw runtime_error("no input file");
	}
	string line;
	while(getline(input,line))
	{
		istringstream stream(line);
		string word;
		bool  firstword=true;
		while(stream>>word)
		{
			map<string,string>::const_iterator map_it=trans_map.find(word);
			if (map_it!=trans_map.end())
			{
				word=map_it->second;
			}
			if (firstword)
			{
				firstword=false;
			}
			else
				cout<<"  ";
			cout<<word;
		}
		cout<<endl;
	}

	system("pause");
	return 0;
}
楼主可以参考下
tofu_ 2013-03-22
  • 打赏
  • 举报
回复
这个是做啥?词典啊? 我测试时,第一个文件内容: Chinese 中文 English 英文 Japanese 日文 第二个文件内容: Japanese English Chinese 完美输出: 日文 英文 中文
tmwanly 2013-03-22
  • 打赏
  • 举报
回复
引用 6 楼 dy106 的回复:
楼主考虑所要打开的文件是否都在改程序的同一目录下
是在同一目录下
加载更多回复(6)

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧