Parsing ASCII Text file into 2d Vector of Char's
up vote
0
down vote
favorite
I need help, im trying to read a file that looks something like this:
.........
.........
.........
.........
....X....
.........
.........
.........
.........
i need to parse this into a 2d vector of chars so i can make modifications to it later.
What ive come up with so far is
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");
for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;
while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}
//for ()
myReadFile.close();
return 0;
}
c++ parsing vector
add a comment |
up vote
0
down vote
favorite
I need help, im trying to read a file that looks something like this:
.........
.........
.........
.........
....X....
.........
.........
.........
.........
i need to parse this into a 2d vector of chars so i can make modifications to it later.
What ive come up with so far is
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");
for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;
while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}
//for ()
myReadFile.close();
return 0;
}
c++ parsing vector
Re:i < data.size()
--data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?
– Pete Becker
Nov 7 at 19:25
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, changeifstream myReadFile; myReadFile.open("input1.txt');
toifstream myReadFile("input1.txt");
.
– Pete Becker
Nov 7 at 19:27
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need help, im trying to read a file that looks something like this:
.........
.........
.........
.........
....X....
.........
.........
.........
.........
i need to parse this into a 2d vector of chars so i can make modifications to it later.
What ive come up with so far is
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");
for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;
while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}
//for ()
myReadFile.close();
return 0;
}
c++ parsing vector
I need help, im trying to read a file that looks something like this:
.........
.........
.........
.........
....X....
.........
.........
.........
.........
i need to parse this into a 2d vector of chars so i can make modifications to it later.
What ive come up with so far is
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");
for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;
while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}
//for ()
myReadFile.close();
return 0;
}
c++ parsing vector
c++ parsing vector
asked Nov 7 at 19:18
xannax159
448
448
Re:i < data.size()
--data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?
– Pete Becker
Nov 7 at 19:25
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, changeifstream myReadFile; myReadFile.open("input1.txt');
toifstream myReadFile("input1.txt");
.
– Pete Becker
Nov 7 at 19:27
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22
add a comment |
Re:i < data.size()
--data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?
– Pete Becker
Nov 7 at 19:25
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, changeifstream myReadFile; myReadFile.open("input1.txt');
toifstream myReadFile("input1.txt");
.
– Pete Becker
Nov 7 at 19:27
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22
Re:
i < data.size()
-- data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?– Pete Becker
Nov 7 at 19:25
Re:
i < data.size()
-- data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?– Pete Becker
Nov 7 at 19:25
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change
ifstream myReadFile; myReadFile.open("input1.txt');
to ifstream myReadFile("input1.txt");
.– Pete Becker
Nov 7 at 19:27
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change
ifstream myReadFile; myReadFile.open("input1.txt');
to ifstream myReadFile("input1.txt");
.– Pete Becker
Nov 7 at 19:27
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;
. But it's probably hard to reserve space before having read the contents.
An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back
), and to use std::string
as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;
. See the following code illustrating this:
#include <sstream>
#include <iostream>
#include <vector>
int main() {
const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";
std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}
lines[2][5] = 'Y';
for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}
Output:
. . . . . . . . .
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
add a comment |
up vote
0
down vote
You may make life easier by using std::getline
and std::string
:
std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}
The std::string
can be accessed using array notation.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;
. But it's probably hard to reserve space before having read the contents.
An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back
), and to use std::string
as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;
. See the following code illustrating this:
#include <sstream>
#include <iostream>
#include <vector>
int main() {
const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";
std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}
lines[2][5] = 'Y';
for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}
Output:
. . . . . . . . .
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
add a comment |
up vote
0
down vote
accepted
You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;
. But it's probably hard to reserve space before having read the contents.
An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back
), and to use std::string
as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;
. See the following code illustrating this:
#include <sstream>
#include <iostream>
#include <vector>
int main() {
const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";
std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}
lines[2][5] = 'Y';
for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}
Output:
. . . . . . . . .
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;
. But it's probably hard to reserve space before having read the contents.
An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back
), and to use std::string
as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;
. See the following code illustrating this:
#include <sstream>
#include <iostream>
#include <vector>
int main() {
const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";
std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}
lines[2][5] = 'Y';
for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}
Output:
. . . . . . . . .
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;
. But it's probably hard to reserve space before having read the contents.
An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back
), and to use std::string
as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;
. See the following code illustrating this:
#include <sstream>
#include <iostream>
#include <vector>
int main() {
const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";
std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}
lines[2][5] = 'Y';
for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}
Output:
. . . . . . . . .
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
answered Nov 7 at 19:45
Stephan Lechner
24.5k21738
24.5k21738
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
add a comment |
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
1
1
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
thank you! i was able to modify your solution to work with text files input by the user.
– xannax159
Nov 8 at 19:06
add a comment |
up vote
0
down vote
You may make life easier by using std::getline
and std::string
:
std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}
The std::string
can be accessed using array notation.
add a comment |
up vote
0
down vote
You may make life easier by using std::getline
and std::string
:
std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}
The std::string
can be accessed using array notation.
add a comment |
up vote
0
down vote
up vote
0
down vote
You may make life easier by using std::getline
and std::string
:
std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}
The std::string
can be accessed using array notation.
You may make life easier by using std::getline
and std::string
:
std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}
The std::string
can be accessed using array notation.
answered Nov 7 at 19:46
Thomas Matthews
43.9k1168120
43.9k1168120
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196324%2fparsing-ascii-text-file-into-2d-vector-of-chars%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Re:
i < data.size()
--data.size()
is 0, so the loop body won't be run. Do you know the dimensions of the grid?– Pete Becker
Nov 7 at 19:25
This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change
ifstream myReadFile; myReadFile.open("input1.txt');
toifstream myReadFile("input1.txt");
.– Pete Becker
Nov 7 at 19:27
@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22