Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Problem while splitting the line using a delimiter and pushing itinto an array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Lionel B, post: 3682134"] Phew, there are so many things wrong with this I don't know where to begin Firstly, if you're to program in C++, better to use the C++ (as opposed to C) headers: they're called things like <iostream>, <fstream>, etc. (no .h). The difference is that they put all functionality in "namespace std" (read up about namespaces). Usually a bad idea to use arrays, particularly with arbitrary sizes (and no error checking in your code). Those are all buffer overflows waiting to happen. Why the extra nested block here? In C++ it's generally viewed as good style to declare variables just before you use them. This doesn't do what you think it does... anyway, this is not the best way to read lines of text (see below) Um... you've not actually *read* the line from the file"! Note that "i" here is not the same "i" you declared above (but j is). [i][i][i] This looks a lot like homework... if it is, the code below should fail you, as there's no way it could be your own work ;-) Read up in particular about the std::string class and std::vector template class. They're probably the most useful things you'll ever use in C++. #include <iostream> #include <string> #include <fstream> #include <vector> // all standard library stuff is in namespace std // Don't use this in a header file, though. using namespace std; int main() { ifstream inf("test.txt"); if (!inf) { cerr << "Failed to open file\n"; return 1; } // read about std::vector vector< vector<string> > a; // read about std::string string line; // This loop works, because getline() returns false (sort of) // when there is nothing more to read. while (getline(inf,line)) { cout << "line = \"" << line << "\"\n"; a.push_back(vector<string>()); // We use find() to find the delimiters and // substr() to yank out the text string::size_type pos1 = 0; string::size_type pos2 = line.find(','); while (pos2 != string::npos) { a.back().push_back(line.substr(pos1,pos2-pos1)); pos1 = pos2+1; pos2 = line.find(',',pos1); } a.back().push_back(line.substr(pos1,pos2-pos1)); } // Now eof should be set if (!inf.eof()) { cerr << "Something went wrong reading file\n"; return 1; } inf.close(); for (size_t i=0;i<a.size();++i) { cout << '\n'; for (size_t j=0;j<a[i].size();++j) { cout << "a[" << i << "][" << j << "] = \"" << a[i][j] << "\"\n"; } } return 0; }[/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Problem while splitting the line using a delimiter and pushing itinto an array
Top