-
May 11th, 2016, 12:41 PM
#16
Re: eof not working properly?
Originally Posted by TheLionKing
ok laserlight, can you please say something about the question I asked you earlier on? about this
auto row_obj = _row{};
Sorry, I missed that. Anyway, the braces form the syntax for the C++11 uniform initialisation.
Originally Posted by 2kaud
Note that the code in my post #4 ignores blank lines so having an empty line at the end (or even multiple blank lines during the file) doesn't effect it's operation.
However, note that 2kaud's suggestion actually ignores lines altogether: if this is undesirable, then you should continue to read line by line with std::getline, but then initialise a stringstream with each line and then use 2kaud's suggestion.
-
May 11th, 2016, 01:07 PM
#17
Re: eof not working properly?
Thank you all soo much..soo very much helpful!!! everybody have a good evening
-
May 12th, 2016, 04:04 AM
#18
Re: eof not working properly?
Originally Posted by 2kaud
Note that the code in my post #4 ignores blank lines so having an empty line at the end (or even multiple blank lines during the file) doesn't effect it's operation.
FWIW, I've found that the issue with not using getline is that it often (wrongly) succeeds with badly structured files. You 'll sometimes end up reading data from multiple lines at once and/or reading mutiple values from a single long line. This most visible for files that contain space delimited representations of matrices. AKA:
Code:
1 2 3
4 5 6
vs
1 2
3 4
5 6
These would look strictly the same to a parser that does "while (ifs >> v1 >> v2) {...}".
I've always found that getline => istringstream to be a solid approach. It's slightly more code, but more robust. With the ws function, it is also not that complicated to check/handle empty lines or trailing data, while ignoring leading/trailing whitespaces.
Code:
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
const string vfn = 1 + R"(
1,2, 3
4 , 5,6
7,8 ,9
)";
struct _row
{
int c1;
int c2;
int c3;
_row(int cc1, int cc2, int cc3) : c1(cc1), c2(cc2), c3(cc3) {}
friend ostream& operator<< (ostream& in_ostream, const _row& w) {
in_ostream << w.c1 << " " << w.c2 << " " << w.c3;
}
};
vector<_row> Wheel;
int main()
{
istringstream ifs(vfn);
int line_number = 0;
string line;
istringstream iss;
int v1 = {};
int v2 = {};
int v3 = {};
char ch;
while (getline(ifs, line)) {
++line_number;
iss.clear();
iss.str(line);
ws(iss);
if (iss.eof()) continue;
ws(iss >> v1 >> ch >> v2 >> ch >> v3);
if (!iss || !iss.eof()) {
cout << "error reading file"
<< " at line " << line_number
<< ": " << line
<< endl;
return EXIT_FAILURE;
}
Wheel.emplace_back(v1, v2, v3);
}
for (const auto& w : Wheel)
cout << w << endl;
}
That said, it really feels like the file is a CSV, in which case a proper CSV parser should be used.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 12th, 2016, 04:41 AM
#19
Re: eof not working properly?
Originally Posted by monarch_dodra
That said, it really feels like the file is a CSV, in which case a proper CSV parser should be used.
thanks for your contribution Monarch, but please what is a CSV? and eventually a csv parser...
-
May 12th, 2016, 04:42 AM
#20
Re: eof not working properly?
Comma Separated Value format
-
May 12th, 2016, 06:22 AM
#21
Re: eof not working properly?
... where each element on a line is separated by a comma delimiter eg
Code:
11,23,45
56,67,89
43,56,11
78,23,54
There can be variations with a different delimiter used - I've seen | used and also multi-char delimiters. For text, " are often used for the element eg
Code:
qw,"rt ty", poi,67
sd,"uio hg", lkj, 89
and if the delimiter occurs within a quote then it is treated as a char and not as a delimiter.
For further info see https://en.wikipedia.org/wiki/Comma-separated_values
Correctly parsing a csv file can be tricky eg as spaces outside of " are ignored etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|