'C++ Problems copying string "buffer" to an vector<vector<string> >
I am new to C++, and I am a little bit overwhelmed by the class structures available, I have no experience with the different librairies hence I am trying to figure out what is the best structures to use for basics tasks.
I am trying to create a function that reads a file and copy it to a working structure, I first tried without any classes or objects, it was not possible to return a valid pointer to a char* [][] structure (don't know if it's the best way to proceed), so I am trying to come up with another solution based on vector<vector > code I picked up somewhere, hopefully it can work this way.
Below is the code I am trying to work out.
void readFile2(FILE* f) {
string buf = "abc";
int a = countLine(f);
int b = countCol(f);
long bytes;
char c;
int d = 0;
fseek(f, 0L, SEEK_END);
bytes = ftell(f);
fseek(f, 0L, SEEK_SET);
int x,y = 0;
vector<vector<string> > vec;
for (int i = 0;i<a;i++) {
vector<string> v1;
for (int j = 0;j<b;j++) {
v1.push_back(buf);
}
vec.push_back(v1);
}
buf.erase();
for (int i=0;i<bytes;i++) {
c = fgetc(f);
if (c == '\t') {
cout<<"buf : "<<buf<<endl;
***cout<<"vec : "<<vec[y][x]<<endl;***
d = 0;
y += 1;
buf.erase();
} else if (c == '\n') {
cout<<"buf : "<<buf<<endl;
**cout<<"vec : "<<vec[y][x]<<endl;**
d = 0;
x += 1;
y = 0;
buf.erase();
} else {
buf.push_back(c);
d++;
}
}
Question :
The vector[y][x] I am trying to fill up with strings coming the buffer in the second part of the function (inbetween asterisks in the above code) does not display anything, although it is correctly initalized with string elements during the initialisation part of the code.
strcpy(vec[y][x],buf) (not present here) does not work either because is requires char* pointer (strange for a function that is named after str, thus I'm kinda stuck because have no idea what to use nor if the structure is correct.
I am trying to design the function to return a valid pointer to the vector that could be used by another function in the main(), I don't know however if it is the correct way or the most efficient way to load the file in memory and generate an array structure.
Thank you in advance for your time.
Solution 1:[1]
A completely new version that is very much straight down the line c++. Your code was basically c code
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
int main() {
std::ifstream input("test.txt");
std::vector<std::vector<std::string>> fields;
int count = 0;
while (input) {
std::string line;
std::getline(input, line);
std::stringstream strs(line);
fields.push_back(std::vector<std::string>());
while (strs) {
std::string field;
std::getline(strs, field, '\t');
fields[count].push_back(field);
}
count++;
}
for (auto& l : fields) {
for (auto& f : l) {
std::cout << f;
}
std::cout << "\n";
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pm100 |
