从.txt [c++]读取代码
reading into code from .txt [c++]
我正试图编写一款冒险游戏,从文本文件上的脚本读取。该程序编译和运行,但它没有从我的。txt文件中读取,我无法弄清楚出了什么问题。
在GetAreaInfo函数中
"fin.seekg(NULL,ios::beg);"显示一个警告:
"main.cpp:92:28: warning:给std::basic_istream<_CharT, _Traits>&的非指针参数1传递NULL;std::basic_istream<_CharT, _Traits>::seekg(std::basic_istream<_CharT, _Traits>::off_type, std::ios_base::seekdir) [with _CharT = char;_Traits = std::char_traits;std:: basic_istream_chart, _Traits>::off_type = long int;std::ios_base::seekdir = std::_Ios_Seekdir]' [-Wconversion-null]"
我不知道如何解决这个问题,但我相信这就是问题所在。如有任何帮助,不胜感激
(代码)//file libraries
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
#define GAMEFILE "world.txt"//read in for the world data
#define CONTINUE 1
#define QUIT 0
//user libraries
struct tArea{//structure that holds the info for the main area
string strCurrentArea;//name of current area
string strDescription;//description of current area
string strNorth;//name of area to the north
string strSouth;//name of area to the south
string strEast;//name of area to the east
string strWest;//name of area to the west
};
//global constants
//function prototypes
void DisplayArea(tArea &area);
void GetAreaInfo(ifstream&,tArea &area);
void Move(ifstream&,tArea &area,string);
int Input(ifstream&,tArea &area);
//execution
int main(){
ifstream fin;//pointer that opens and reads from file
tArea area;//area structure data
fin.open(GAMEFILE);//
if(fin.fail()){
cout<<"UNABLE TO FIND GAME FILEn";
return -1;
}
fin>>area.strCurrentArea>>area.strCurrentArea;//reads in the start point for the game
GetAreaInfo(fin,area);//calls function to receive info from the file
DisplayArea(area);//calls function to display the area to the user
while(1){//main game loop
if(Input(fin,area)==QUIT){//if user enters quit, game ends
break;
}
}
fin.close();//closes file
Sleep(1000);//1 second delay before ending
return 0;
}
/**********************************************************/
//*****************DisplayArea*****************************/
//******Function is called to display area description*****/
/**********************************************************/
void DisplayArea(tArea &area)
{
cout<<area.strDescription<<endl<<endl;
}
/**********************************************************/
//*****************GetAreaInfo*****************************/
//******Function is called to read in the area*************/
//************information from the file********************/
/**********************************************************/
void GetAreaInfo(ifstream &fin,tArea &area){
string strTemp="";
string strTemp2="";//temporary for reading in info
string strArea="<"+area.strCurrentArea+">";//looks for the room name in brackets to find easier i.e instead of main it reads in <main>
fin.seekg(NULL,ios::beg);//starts the header search from the beginning of the file
fin.clear();//allows the file to be read through multiple times
while(getline(fin, strTemp, 'n')){//while loop reads file til it finds the correct area heading
if(strTemp==strArea){
getline(fin, area.strDescription, '*');//if it finds the correct area heading, it reads its info
// Read past the direction blocks (I.E. <north>) and store the room name for that direction
fin>>strTemp>>area.strNorth;
fin>>strTemp>>area.strEast;
fin>>strTemp>>area.strSouth;
fin>>strTemp>>area.strWest;//it then read in the surrounding area titles by skipping their <area> descriptors
return;
}
}
}
/**********************************************************/
//*********************Move********************************/
//******Function is called to move through the*************/
//****game if there is an area in that direction***********/
/**********************************************************/
void Move(ifstream &fin,tArea &area, string strArea){
if(strArea=="None"){//detects if there is no area in the direction inputted
cout<<"You are unable to travel that wayn";//displays error message and returns the function
return;
}
else{
area.strCurrentArea=strArea;// Sets the current area to the new one
GetAreaInfo(fin, area); // Passes in the file pointer so the new area data is read
DisplayArea(area);// Displays current area
}
}
/**********************************************************/
//*********************Input*******************************/
//******Main game mechanic feature. Receives **************/
//****user input and reacts to it accordingly to **********/
//*******progress through the game*************************/
/**********************************************************/
int Input(ifstream &fin, tArea &area){
string strInput="";//holds user input
cout<<endl<<":";//displays prompt
cin>>strInput;//reads in the user input
if(strInput=="look"){
DisplayArea(area);//calls function to give current area description
}
else if(strInput=="north"){
Move(fin,area,area.strNorth);//calls function to move north if possible
}
else if(strInput=="east"){
Move(fin,area,area.strEast);//calls function to move east if possible
}
else if(strInput=="west"){
Move(fin,area,area.strWest);//calls function to move west is possible
}
else if(strInput=="south"){//calls function to move south if possible
Move(fin,area,area.strSouth);
}
else if(strInput=="help"||strInput=="?"){//displays commands to user
cout<<"Commands: north east west south look quit helpn";
}
else if(strInput=="quit"){//ends game
cout<<"Quit game?n";
return QUIT;
}
else{//displays when invalid input is received
cout<<"Invalid inputn";
}
return CONTINUE;
}
iostream::seekg
具有以下签名:
-
istream& seekg (streampos pos);
-
istream& seekg (streamoff off, ios_base::seekdir way);
NULL
是一个空指针常量。
你可能想写:
fin.seekg(0, ios::beg);
或简单的:
fin.seekg(0);
相关文章:
- 读取最后一行代码算法 - c++ 时出现问题
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 如何使用指针直接从托管代码中的非托管代码中读取矢量数据
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 如何只读取和剥离特定的代码行
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 从C++代码中直接读取成员属性
- 驱动程序,以支持从 C++ 代码读取或写入 HIVE
- 读取代码中的文件版本值
- 为什么抓取窗口标题的代码会导致应用崩溃?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 从文本文件中读取的代码,删除停止语,然后应用大小写折叠
- 有关要从结构指针读取的代码的简单代码
- Python等效于从stdin读取的C++代码
- 为什么我无法读取带有代码::块C++的文件?
- 如何从子进程读取返回代码
- 干净的方式,使可移植的端正确的文件读取/编写代码在c++