I need to read a special file, and the first line is two number the number of rows and number of column and the next is some text. Like:
我需要读一个特殊的文件,第一行是两个数字,行数和列数,然后是一些文本。如:
2 2
ab
cd
What I need to do make a 2-d array and put the text in to array, like array[2][2]
, and all text must be in the array. This is what I do now.
我需要做的是制作一个二维数组,将文本输入到数组中,比如数组[2][2],所有文本都必须在数组中。这就是我现在所做的。
if stream file;
file.open("name.in")
char array[][]
1 个解决方案
#1
0
When you could use std::string this is a easy solution. This is the code for reading the first line of your file.
当您可以使用std::string这是一个简单的解决方案。这是读取文件第一行的代码。
fstream file;
file.open("name.in", ios::in|ios::out);
string line;
getline(file,line);
char help= line.at(0);
With char help you could extract the number with the function std::stoi.
使用char帮助您可以用std::stoi来提取数字。
For reading the hole file until the end you could these code lines
要阅读这个孔文件,直到最后你可以使用这些代码行。
while (getline(file, line) ) {
// Do your Stuff with the text
}
#1
0
When you could use std::string this is a easy solution. This is the code for reading the first line of your file.
当您可以使用std::string这是一个简单的解决方案。这是读取文件第一行的代码。
fstream file;
file.open("name.in", ios::in|ios::out);
string line;
getline(file,line);
char help= line.at(0);
With char help you could extract the number with the function std::stoi.
使用char帮助您可以用std::stoi来提取数字。
For reading the hole file until the end you could these code lines
要阅读这个孔文件,直到最后你可以使用这些代码行。
while (getline(file, line) ) {
// Do your Stuff with the text
}