Home » U++ Library support » U++ Core » GetCsvLine (Reading CSVfile)
Re: GetCsvLine [message #50784 is a reply to message #50783] |
Tue, 18 December 2018 13:00 |
Oblivion
Messages: 1093 Registered: August 2007
|
Senior Contributor |
|
|
Hello bozero,
There are many ways to achive this, but given your example, the below code would hopefully give you the basic idea:
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
const String csvstr = "data1,data2,data3\ndata4,data5,data6\ndata7,data8,data9";
auto lines = Split(csvstr, '\n');
for(int i = 0; i < lines.GetCount(); i++) {
String line = lines[i];
Cout() << "Line: " << i + 1 << ": \n";
StringStream ss(line);
auto v = GetCsvLine(ss, ',', CHARSET_DEFAULT);
for(auto e : v)
Cout() << e << '\n';
}
}
Another simple way:
#include <Core/Core.h>
using namespace Upp;
CONSOLE_APP_MAIN
{
const String csvstr = "data1,data2,data3\ndata4,data5,data6\ndata7,data8,data9";
StringStream ss(csvstr);
int ln = 0;
while(!ss.IsEof()) {
auto s = ss.GetLine();
StringStream line(s);
auto v = GetCsvLine(line, ',', CHARSET_DEFAULT);
Cout() << "Line: " << ++ln << "\n";
for(auto e : v)
Cout() << e << '\n';
}
}
These output:
Line: 1:
data1
data2
data3
Line: 2:
data4
data5
data6
Line: 3:
data7
data8
data9
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Tue, 18 December 2018 13:17] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri May 03 22:36:29 CEST 2024
Total time taken to generate the page: 0.01666 seconds
|