简体   繁体   中英

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

I am trying to create a maze map in txt file

here is the.txt file

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

7 and 7 are the number of rows and columns respectively. The spaces are the contents of the array too/ how can I print the spaces in 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

Here is an example with 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(); 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). 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. 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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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