-2

Hi I have a text file which is supposed to be data for a new game item to be created it looks like this:

1 Sword 5 10
2 Bow 5 10
3 Axe 5 10
4 Shield 5 10
5 Spear 5 10

I know how to read each individual piece separated by space and store them into variables, but what if I wanted to use more than 1 word for the name, like "Big Sword", or "Heavy Bow", and store that as a single string.

Right now for each line I'm using

file >> id >> name >> value >> value

But is there a way to make it so that when it gets to the "name" part, I can specify how many words it will store into a single string, and also can it be done with a flexible amount of words like "Big sword" or "Big Rusty Sword" or "Big Rusty Heavy Sword" and it would still know that's one big name.

I haven't come up with any ideas yet

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • If your delimiter can appear in your individual "pieces", you either have to implement quoting or escaping semantics, or just change your delimiter. – Botje Aug 23 '23 at 13:58
  • Alternatively, if you only have one such piece you can put it at the end of the line and use `std::getline` to grab the remainder of the line. – Botje Aug 23 '23 at 13:59
  • 2
    You need to change your file format. The reason you haven't come up with any ideas is because your format is vague (at best) or inherently ambiguous (at worst). If you can change the format then change it, if you cannot change the format, then ask the person who designed this format what you should do. – john Aug 23 '23 at 14:00
  • 1
    A simple fix that occurs to me is to replace the spaces with underscores e.g. `1 Big_Rusty_Heavy_Sword 5 10` then you can replace the underscores with spaces after you have read the string. – john Aug 23 '23 at 14:02

1 Answers1

1

If your compiler supports C++17 then a simple way is to use manipulator std::quoted declared in header <iomanip> that allows to store a string in quotes.

Here is a demonstration program. Instead of using file streams there are for simplicity used string streams.

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

int main()
{
    std::ostringstream os;

    os << 1 << ' ' << std::quoted("Big Rusty Heavy Sword") << ' ' 
        << 5 << ' ' << 10;

    unsigned int id;
    std::string name;
    int value1, value2;

    std::istringstream is( os.str() );

    is >> id >> std::quoted( name ) >> value1 >> value2;

    std::cout << "id = " << id << ", name = " << name
        << ", value1 = " << value1 << ", value2 = " << value2
        << '\n';
}

The program output is

id = 1, name = Big Rusty Heavy Sword, value1 = 5, value2 = 10

That is you need to use the manipulator std::quoted to store a multi-word string and then to use the manipulator to read the string.

For example you can read a whole line from a file using standard function std::getline and then use an input string stream based on the line to read separate items.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335