简体   繁体   English

如何从 c++ 中的文件中读取字符数组(有一些空间)

[英]how read char array(with some space) from file in c++

I am trying to create a maze map in txt file我正在尝试在 txt 文件中创建一个迷宫 map

here is the.txt file这是.txt文件

7 7
e%     
 %% %% 
 %% %%%
%%% %%%
  %   %
%   %  
x % %% 

7 and 7 are the number of rows and columns respectively. 7 和 7 分别是行数和列数。 The spaces are the contents of the array too/ how can I print the spaces in c++空格也是数组的内容/我如何打印 c++ 中的空格

I have tried to code for it but it doesn't work with space:我试图为它编写代码,但它不适用于空间:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    ifstream map("m.txt");
    if (!map) {
        cout << endl << "Failed to open file";
        return 1;
    }

    int rows = 0, cols = 0;
    map >> rows >> cols;

    vector<vector<char> > arr(rows, vector<char>(cols));
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            map >> arr[i][j];
        }
    }
    map.close();

    cout << "This is the grid from file: " << endl;
    for (int i = 0; i < rows; i++)
    {
        cout << "\t";
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j];
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

first time to ask question hope you guys can get the point thanks a lot for helping第一次问问题希望大家能明白 非常感谢帮助

map >> arr[i][j]; is a formatted input.是格式化的输入。 It skips whitespaces.它跳过空格。 You have to use a different method, eg std::basic_istream<CharT,Traits>::get or std::basic_istream<CharT,Traits>::getline您必须使用不同的方法,例如std::basic_istream<CharT,Traits>::getstd::basic_istream<CharT,Traits>::getline

Here is an example with get()这是get()的示例

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    ifstream map("m.txt");
    if (!map) {
        cout << endl << "Failed to open file";
        return 1;
    }

    int rows = 0, cols = 0;
    map >> rows >> cols;
    // Skip linebreak after line: 7 7
    map.ignore();

    vector<vector<char> > arr(rows, vector<char>(cols));
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            // Read each char, also whitespaces and linebreaks
            arr[i][j] = map.get();
        }
        // Skip linebreak
        map.ignore();
    }
    map.close();

    cout << "This is the grid from file: " << endl;
    for (int i = 0; i < rows; i++)
    {
        cout << "\t";
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j];
        }
        cout << endl;
    }

    return 0;
}

I had to add two map.ignore();我不得不添加两个map.ignore(); because the line因为线

map >> arr[i][j];

skipped all linebreaks but跳过所有换行符,但

arr[i][j] = map.get();

would read them so we have to manually skip them.会阅读它们,因此我们必须手动跳过它们。

To better clarify my answer (as Yunnosch asked).为了更好地澄清我的答案(正如 Yunnosch 所问的那样)。 My point wasn't to solve all problems, only to point at the problem of why the initial code does not work.我的意思不是解决所有问题,只是指出为什么初始代码不起作用的问题。 True, I didn't clarify, I only posted some "new" code.没错,我没有澄清,我只发布了一些“新”代码。

Original code posted by Cynthia doesn't work because operator>> reads all characters until the first space. Cynthia 发布的原始代码不起作用,因为operator>>会读取所有字符,直到第一个空格。 My approach was to read the whole line and then break it to the same nested vector as in the initial code.我的方法是读取整行,然后将其分解为与初始代码中相同的嵌套向量。 Be aware that this also reads and stores "7 7" line as part of arr请注意,这也会读取并存储“7 7”行作为arr的一部分

Edit: I had to add a few semicolons for it to compile, and I removed 'reserve' since it can only confuse fresh programmers.编辑:我必须添加几个分号才能编译,并且我删除了“保留”,因为它只会让新程序员感到困惑。

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

int main()
{
    ifstream map("m.txt");
    if (!map) {
        cout << endl << "Failed to open file";
        return 1;
    }

    vector<vector<char> > arr;
    string line;
    // no need for size at start, we can deduce it from line size
    while(getline(map, line))
    {
        vector<char> temp;
        for (auto c : line)
            temp.push_back(c);
        arr.push_back(temp);
    }
    map.close();

    cout << "This is the grid from file: " << endl;
    // you can always get number of rows and cols from vector size
    // additionally, each line can be of different size
    for (int i = 0; i < arr.size(); i++)
    {
        cout << "\t";
        for (int j = 0; j < arr.at(i).size(); j++)
        {
            cout << arr.at(i).at(j);
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM