I'm curious to see if someone can give me an idea for an alternative to
getline() for reading files.
The .txt file looks like this:
A: IoT device A with data ialNzXFercGED
B: IoT device B with data ialNzXFercGEDWQ
C: IoT device C with data x4vIHjv5U
D: IoT device D with data MBZXRcqOUseiK
E: IoT device E with data FzaMHyi7
F: IoT device F with data FzaMHyi7xD
I want to read the end string after "data " and store it in a variable so I can check the size of the strings in bytes. I would use getline() but it seems to be a long winded approach.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
usingnamespace std;
int main()
{
// ifstream in( "data.txt" );
stringstream in( "A: IoT device A with data ialNzXFercGED \n""B: IoT device B with data ialNzXFercGEDWQ \n""C: IoT device C with data x4vIHjv5U \n""D: IoT device D with data MBZXRcqOUseiK \n""E: IoT device E with data FzaMHyi7 \n""F: IoT device F with data FzaMHyi7xD \n" );
string dummy;
vector<string> stuff;
while ( in >> dummy )
{
if ( dummy == "data" )
{
string item;
in >> item;
stuff.push_back( item );
}
}
cout << "The truly important stuff is\n";
for ( string s : stuff ) cout << s << '\n';
}
The truly important stuff is
ialNzXFercGED
ialNzXFercGEDWQ
x4vIHjv5U
MBZXRcqOUseiK
FzaMHyi7
FzaMHyi7xD
Ok cool thanks everybody, I'll give the ideas you gave a shot.
The idea for the assignment is memory allocation. Basically storing those strings in different sized "blocks" within an array which is the memory location in theory.
if you KNOW that the data is fixed width, its even easier, you can take a direct hard coded substring out of the getline (starting point is hard, anyway, endpoint is end of string).
Otherwise, if not fixed width, i would use Duthomas' approach.
markup would make this simple file bloat to 4x its current size or worse. IMHO markup is for nested capability and just makes a mess for simple data.