Alternative for getline()

Hi

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.

Is there a better way of doing this ?

Thanks
There isn't. getline() is your bread-and-butter function.

However, in this specific case, if your file is guaranteed to have exactly that format, you can use the formatted input.

1
2
3
4
5
  string foo, sn;
  while (f >> foo >> foo >> foo >> foo >> foo >> foo >> sn)
  {
    std::cout << sn.length() << "\n";
  }


That said, doing all that string processing would be significantly less efficient than the simple getline() and a single reverse find:

1
2
3
4
5
6
  string s;
  while (getline( f, s ))
  {
    auto n = s.rfind( ' ' );
    std::cout << (s.length() - n + 1) << "\n";
  }

Hope this helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace 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
You could also use json, xml or ini files.
Then in reality you would wanna use something like sql.

In the real world nobody would bother parsing files like that.

edit: Towards jonnins point, markup languages was not what I intended to suggest, all 3 options I offered are severly different from eachother. XML is a markup language (my bad), JSON is a data interchange language, and INI is a configuration file, but all 3 are capable of doing the same thing, store very basic data. If you care about having a simple layout, only XML has the rep of "looking" fat to some people, json and ini are overall are very slim and direct. This is mostly useful if you are using A, B, C style indexes because doing that yourself can be a huge pain in the butt since that functionality is built in those formats, and all the other suggestions are not bullet proof from typos and errors (if you leave a space at the end of a line getline+rfind will screw up), and the library will also give the exact line whenever the syntax error occurs (usually), but although you would still wanna error check the document result since you would want to check if there are no spaces or whatever else is the requirement. But at the end, it doesn't matter since this is a homework assignment 💩
Last edited on
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.
In the real world nobody would bother parsing files like that.

I think you would be amazed (or maybe distressed) at what happens in the "real world".
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.
Last edited on
thanks all, I got it work as I wanted :)
Topic archived. No new replies allowed.