C++ 硬件帮助-逐字读取文件中的字符C++;

C++ 硬件帮助-逐字读取文件中的字符C++;,c++,C++,对于我当前正在处理的项目,我必须从文件中读取数据,并根据文件中的特定字符,将1或0输出到数组中 下面是一个文件输入示例: * * * * * * ** ** ** *** * 下面是我为处理这个问题而编写的函数: void input (int cellGrid[][MAX]) //takes info from a .txt and puts it into an array { ifstream infile; //declare a file variable int

对于我当前正在处理的项目,我必须从文件中读取数据,并根据文件中的特定字符,将1或0输出到数组中

下面是一个文件输入示例:

*  *   *
*  *  * 
** ** **
 ***   *
下面是我为处理这个问题而编写的函数:

void input (int cellGrid[][MAX]) //takes info from a .txt and puts it into an array
{
ifstream infile; //declare a file variable
int row;
int column;
int number;
infile.open("life.txt"); //open a file


while(infile>>row>>column) { //inserts bacteria places into array
cout << row << " " << column << endl;

cellGrid[row][column]=1; //makes it equal one if bacteria is present
}

infile.close(); //closes file
}
void输入(int-cellGrid[][MAX])//从.txt获取信息并将其放入数组中
{
ifstream infle;//声明一个文件变量
int行;
int列;
整数;
infle.open(“life.txt”);//打开一个文件
while(填充>>行>>列){//将细菌位置插入数组

我的想法是这样的:

set row to 0
while can read a line from file
    set column to 0
    for each character on line
        if character  is '*'
            set cellGrid(row,column) to 1
        else
            set cellGrid(row,column) to 0
        increment column
    increment row

您可能需要额外的逻辑来捕获行或列,试图超出界限或字符不是“或”*。

< P>一般在C++中使用<代码> STD::向量< /代码>尽可能。

阵列示例: 您必须遍历文件并记录每个
*
的位置。然后将该位置设置为1。如下所示(我们使用
getline
i
作为行的计数器,然后使用
j
作为列的计数器循环行):

向量示例#2: 函数最好返回一个新创建并填充的
向量

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

vector<vector<int>> input() {
    ifstream infile;
    infile.open("life.txt");
    vector<vector<int>> cell_grid;

    int i = 0;
    for (string line; getline(infile, line); ++i)
    {
        cell_grid.push_back(vector<int>(line.size()));
        for (size_t j = 0; j < line.size(); ++j) {
            if (line[j] == '*') {
                cell_grid[i][j] = 1;
            }
        }
    }

    infile.close();
    return cell_grid;
}

int main() {
    auto vec = input();
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
向量输入(){
河流充填;
infle.open(“life.txt”);
矢量单元网格;
int i=0;
for(字符串行;getline(填充,行);+i)
{
单元格网格。向后推(向量(line.size());
对于(大小j=0;j >从<代码>中读取的代码  >与您的示例输入文件完全不匹配。您的代码试图读取整数,但您的输入文件中没有整数。请考虑从读取<代码> */COD>作为整数时所得到的数字。考虑使用<代码> STD::GETLION<代码>获取一行和计数。g行上的字符以获得列数。循环该循环,直到您无法再读取并知道有多少行。现在您可以构建一个单元格数组。使用
std::vector
来存储行可能比使用任意固定大小的C样式数组更好。您发布的解决方案可行,但如果有100多个列呢?是吗无法动态分配2D数组?@RishavSinha使用返回
向量的示例编辑。
#include <fstream>
#include <string>
#include <vector>
using namespace std;

void input(vector<vector<int>> &cellGrid) {
    ifstream infile;
    infile.open("life.txt");

    int i = 0;
    for (string line; getline(infile, line); ++i)
    {
        cellGrid.push_back(vector<int>(line.size()));
        for (size_t j = 0; j < line.size(); ++j) {
            if (line[j] == '*') {
                cellGrid[i][j] = 1;
            }
        }
    }

    infile.close();
}

int main() {
    vector<vector<int>> cellGrid;
    vector<vector<int>> cellGrid2(100);
    input(cellGrid);
    //input(cellGrid2); - THIS WILL THROW AN EXCEPTION
    return 0;
}
#include <fstream>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> input() {
    ifstream infile;
    infile.open("life.txt");
    vector<vector<int>> cell_grid;

    int i = 0;
    for (string line; getline(infile, line); ++i)
    {
        cell_grid.push_back(vector<int>(line.size()));
        for (size_t j = 0; j < line.size(); ++j) {
            if (line[j] == '*') {
                cell_grid[i][j] = 1;
            }
        }
    }

    infile.close();
    return cell_grid;
}

int main() {
    auto vec = input();
    return 0;
}