I am doing a school project where we must not use std::string
. How can I do this? In the txt file the data are separated with a ";", and we do not know the length of the words.
Example:
apple1;apple2;apple3
mango1;mango2;mango3
I tried a lot of things, but nothing worked, always got errors.
I tried using getline, but since it is for string it did not work.
I also tried to reload the operator<<
but it did not help.
There are two entirely separate getline()
's. One is std::getline()
, which takes a std::string
as a parameter.
But there's also a member function in std::istream
, which works with an array of char
s instead of a std::string
, eg:
#include <sstream>
#include <iostream>
int main() {
std::istringstream infile{"apple1;apple2;apple3"};
char buffer[256];
while (infile.getline(buffer, sizeof(buffer), ';'))
std::cout << buffer << "\n";
}
Result:
apple1
apple2
apple3
Note: while this fits the school prohibition against using std::string
, there's almost no other situation where it makes sense.