2014-01-05 128 views
-1

我试图创建一个系统中,我可以从.txt文件中读取,然后对什么是发现行为。所以说,我想打一个级别,我就不必硬编码每块,我可以只让一个.txt文件,说是这样的:读取数据从.txt(C++)

ooooo 
ooooo 
oxxxo 

从中读取,并创建一个基于水平关闭的。这一切都很好,很快乐,期望我不懂如何从文本文档中读取内容并根据其内容采取行动。 (虽然我知道这不是最好的,最理想的,或非常干净的方式,但并非如此,我很抱歉看到这种“蛮力”效果,请谅解)

我写了这个小程序前一段时间,将输入用于字符串,打开.txt文件并写入用户输入的内容。我相信这与我想从文本文档中读取的方式非常相似。

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

string input; 

int main(){ 
    ofstream textFile; 
    cout << "Enter text to write to file: " << endl; 
    getline(cin, input); 
    textFile.open ("example.txt"); 
    textFile << "\n" << input; 
    textFile.close(); 
    return 0; 
} 

也许如果你可以创建一个基于此的例子解释如何做到这一点?

预先感谢您。

+0

你的问题不是很清楚。你想根据* what *创建一个关卡 – 0x499602D2

+0

我建议你搜索“[C++]从文件读取”的StackOverflow,因为这个问题每周都会出现,并且已经被回答了很多次。 –

回答

0

如果你的意思是你想知道如何从文件读取,请检查我写的代码。

example.txt中

hello 
who are you? 

代码:

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
using namespace std; 

int main(){ 

    string line, cell; 
    ifstream myfile("example.txt"); 
    // try to open the file, else if you cannot throw exception 
    try 
    { 
     myfile.is_open(); 

      // read each line into line 
      while (getline(myfile, line)) 
      { 
       cout << "Line contains: " << line << endl; 
       // copy the line into lineSteam, so we can store them into a string delimited 
       stringstream lineStream(line); 
       while (getline(lineStream, cell, ' ')) 
       { 
        cout <<"Words delimited by space: "<< cell << endl; 
       } 
      } 
    } 
    // display the exception thrown 
    catch (ifstream::failure e) { 
     cout << "Exception opening/reading file: " << e.what(); 
    } 

    return 0; 
} 
+1

您应该鼓励OP先搜索Web和堆栈溢出。从文件中读取是一个很受欢迎的问题,并多次回答。 –

+0

我同意这一点。 – meWantToLearn

0

我假设你想从你的文本文件,基于网格的水平?您将需要一个while循环,将文本文件中的行读入字符串,并使用int来跟踪您已完成多少次循环。

在while循环中,您需要一个for循环,它将遍历刚才读取的字符串中的每个字符(getLine)。使用这个角色,你可以决定,如果它是'x',那么你可以把墙放在适当的网格位置。

通过跟踪的多少,而循环迭代你做了,什么性格你是字符串中读取,这会给你一个网格位置的位置(即3,2)。

我做了类似的比赛,我在天发回来的东西。

Tile *** TileGrid; 

//load in the file 
std::ifstream levelData; 
levelData.open(fileName); 

std::string levelInfo; 

//loop through all the lines, counting the height of the map 
TilesTall = 0; 
while (levelData.eof() == false) { 

    std::getline(levelData, levelInfo); 
    TilesTall++; 
} 

//get the last line of the map data and count how many characters there are across for the tiles wide 
TilesWide = levelInfo.size(); 

//reset the file line pointer 
levelData.clear(); 
levelData.seekg(0, std::istream::ios_base::beg); 

//using this info we can now create our tile grid 
TileGrid = new Tile**[TilesTall]; 
for (int i = 0; i < TilesTall; i++) 
    TileGrid[i] = new Tile*[TilesWide]; 

//now read in the rest of the level data 
for (int row = 0; row < TilesTall; row++) { 

    std::string loopedString; 
    std::getline(levelData, loopedString); 

    //loop through this lines characters getting the type from the char 
    for (int i = 0; i < TilesWide; i++) { 

     char indexChar = loopedString[i]; 
     CoverTypes type; 

     bool isSpawnPoint = false; 

     //parse the cover type 
     switch (indexChar) { 

     case 'L': 
      type = kLow; 

      break; 
     case 'H': 
      type = kHigh; 

      break; 
     case 'P': 
      type = kOpen; 
      isSpawnPoint = true; 

      break; 
     default: 
      type = kOpen; 

      break; 
     } 

     //create the tile 
     TileGrid[row][i] = new Tile(type, i, row, this); 

     if (isSpawnPoint == true) 
      _spawnPositions.push_back(TileGrid[row][i]); 
    } 
} 
+0

我已经将一些变量从公共变量中去掉了局部变量,所以不要留意随机大写变量 – user819640

+0

对于TilesWide:would not doing levelInfo.size();只是返回文件的大小?喜欢,如果它是 ooooo ooooo oxxxo 你会得到15吗?我错了吗? – user3163278

+0

Dunno dude,试试吧:)我并没有将代码粘贴为解决方案,而只是为了让你朝正确的方向前进。您需要根据您的文本文件的样式进行修改。 – user819640