im trying to read txt file to my char array[9][9]. I tried some tips from other topics, but they didn't work.
So, I have an example file.txt like this, which I saved myself, like this: it is 9x9
123------
---456---
------789
and so on, whitespaces here are ' ' instead of '-'. When I'm trying to read it, and put it to my array again, it is displayed like this:
123456789.
Here's my code:
system("cls");
ifstream sudoku_read;
string name_read;
cout << "Put a text name: "
getline(cin, name_read);
sudoku_read.open(name_read, ios::in | ios::binary);
for (int i = 0; i < 9;i++)
{
for (int j = 0; j < 9; j++)
{
sudoku_read >> tab[j][i].number;
}
}
I tried noskipws also but it does not work correctly.
char crs[100];
unsigned vindex = 0;
std::stringstream ss;
ss = sudoku_read.rdbuf();
std::string flstr = ss.str();
for(int i=0; i<strlen(flstr); i++){
if(flstr[i] != ' '){
crs[vindex++] = flstr[i];
}
}
vindex = 0;
for (int i = 0; i < 9;i++)
{
for (int j = 0; j < 9; j++)
{
tab[j][i].number = crs[vindex++] - '0'; //watch out it should be [i][j] by convention
}
}
I have conventionally set crs' size to 100 since it is unspecified.
Assumptions:
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.