本文介绍了读取TXT文件字符在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,从一个txt文件中读取行数和列数。此外,程序具有从同一文件中读取的二维数组的内容。

下面是TXT文件

  8月20日
 *
  *
***
         ***

8和20是行数和列数分别。在空间和星号是数组的内容,数组[8] [20] 例如,数组[0] [1] ='*

我做使程序读取8和20如下:

  ifstream的MYFILE;
myFile.open(life.txt);如果(!MYFILE){
    COUT<< ENDL<< 无法打开文件;
    返回1;
}MYFILE>>行>> COLS;
myFile.close();格=新的char * [行]
的for(int i = 0; I<行;我++){
    电网[I] =新的char [COLS]
}

现在,如何分配的空间和星号向阵列中的字段?

我做了以下,但没有奏效。

 的for(int i = 0; I<行;我++){
        对于(INT J = 0; J< COLS; J ++){
            而(MYFILE>> CH)
            {
            电网[I] [J] = CH;
            }
        }
    }

我希望你有这一点。


解决方案

 的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;矢量>使用命名空间std;诠释的main()
{
    ifstream的MYFILE(file.txt的);    如果(!MYFILE){
      COUT<< ENDL<< 无法打开文件;
        返回1;
    }    INT行= 0,COLS = 0;
    MYFILE>>行>> COLS;    矢量<矢量<焦炭> >格(行,矢量<焦炭>(COLS));
    的for(int i = 0; I<行;我++)
    {
        对于(INT J = 0; J< COLS; J ++)
        {
            如果(myFile.fail()){COUT&所述;&下; 文件中的数据不正确<< ENDL;}
            MYFILE>>电网[I] [J]。
        }
    }
    myFile.close();    //打印回格
    性病::法院LT&;< 这是文件中的网格:<< ENDL;
    的for(int i = 0; I<行;我++)
    {
        COUT<< \\ t的;
        对于(INT J = 0; J< COLS; J ++)
        {
            COUT<<电网[I] [J]。
        }
        COUT<< ENDL;
    }
}

I have a program that will read the number of rows and columns from a txt file. Also, the program has to read the contents of a 2D array from the same file.

Here is the txt file

8 20
 *
  *
***


         ***

8 and 20 are the number of rows and columns respectively. The spaces and asterisks are the contents of the array, Array[8][20] For example, Array[0][1] = '*'

I did make the program reading 8 and 20 as follow:

ifstream myFile;
myFile.open("life.txt");

if(!myFile) {
    cout << endl << "Failed to open file";
    return 1;
}

myFile >> rows >> cols;
myFile.close();

grid = new char*[rows];
for (int i = 0; i < rows; i++) {
    grid[i] = new char[cols];
}

Now, how to assign the spaces and the asterisks to to the fields in the array?

I did the following, but it didn't work

for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            while ( myFile >> ch )
            {
            grid[i][j] = ch;
            }
        }
    }

I hope you got the point.

解决方案
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream myFile("file.txt");

    if(!myFile) {
      cout << endl << "Failed to open file";
        return 1;
    }

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

    vector<vector<char> > grid(rows, vector<char>(cols));
    for(int i = 0;i < rows;i++)
    {
        for(int j = 0;j < cols;j++)
        {
            if(myFile.fail()) {cout << "Improper data in file" << endl;}
            myFile >> grid[i][j];
        }
    }
    myFile.close();

    //Printing the grid back
    std::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 << grid[i][j];
        }
        cout << endl;
    }
}

这篇关于读取TXT文件字符在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:19
查看更多