c/c++有关单词的读取

来源:百度知道 编辑:UC知道 时间:2024/06/15 10:19:01
我将很多个单词放到一个TXT文档中,一行放一个单词(包括单词的中文解释),单词与中文解释之间用“-”分开的。我想从文档中随机读取单词,并且当我输出“4”时,能够显示读取单词的中文解释。
希望能够写得详细点,谢谢!

楼上的是C语言的,我的是CPP的 CPP一定要有CPP的样子

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>
#include <conio.h>
using namespace std;

#define kEY_Q 113
#define KEY_NUM_4 11
#define KEY_SPACE 32
#define KEY_Y 121
#define KEY_y 89
int main(int argc, char* argv[])
{
string fname;//获取文件名
cout<<"please input the file path and name:";
cin>>fname;
//如果获取文件名失败,则使用默认值
if (fname.length()==0)
{
string fname="aa.txt";
}
cout<<"是否要录入新单词? Y / N ?"<<endl;
int x=_getch();//获取按键,判断是否录入单词
cout<<x;
if (x==KEY_Y||x==KEY_y)
{
cout<<"输入end 结束单词录入"<<endl;
//打开文件,循环录入新单词,并写进文件
ofstream out(fname.c_str(),ios::app);
string strIn;
cin>>strIn;
while (st