how to assign characters with empty spaces into single string

If i have

potatoe onion tomatoe yeah
somewhere in txt

and I know that it's total of 26 characters, how do I store it to a single simple string with no wise-ass approach?
Last edited on
std::string myString = "potatoe onion tomatoe 1234";

Edit: Bonus points for the Quaylism :)
Last edited on
Not sure trolling or not :D

I have them in txt and i want to tell compiler to read 26chars into a string. It can be anything with random amount of blank spaces.

It doesn't work the seemingly obvious way:

1
2
string name;
in.getline(name, 26);


Last edited on
A blank space is a character so 26 chars will include spaces. Instead, you should read in the input and count your own "non-space" characters.

Try:

1
2
3
4
5
6
7
8
std::string input;
std::ifstream ifileStream("filename");
// If my file looks like:
// Some     text
ifileStream >> input;
// input = "Some"
ifileStream >> input
// input = "text" 

That will take in characters, delimited by whitespace (whitespace will be ignored)
Not sure trolling or not :D

No troll. You asked about assigning characters that include spaces to a string, and I posted a way to do it. If you'd actually asked about what you really want to know - about reading text from a file - then you would have gotten the answer you were looking for.

We can't read your mind to know what you really meant. All we have is the words you actually chose to type.
Instead, you should read in the input and count your own "non-space" characters.


Not an option, it should work with any data. We have 26 chars of random text and some variables on each line. Maybe somehow tell it to read into a string until it sees variables (unlike in mine given example earlier, text does not contain numbers)?

But how?
Last edited on
It depends on what exactly is the question.

If you just want to read a line of text from a file, then getline() is the simplest solution.
1
2
3
4
5
    ifstream fin("test.txt");

    string line;
    getline(fin, line);
    cout << line << endl;


But if you want to read precisely 26 characters, then read() may be better
1
2
3
4
5
6
    ifstream fin("test.txt");

    char buffer[27];
    fin.read(buffer,26);
    buffer[26] = 0;         // add null terminator
    cout << buffer << endl;


Maybe somehow tell it to read into a string until it sees variables

Please explain what this means. How is a 'variable' to be recognised?
And do you not in fact want to read exactly 26 characters after all?
Last edited on
But if you want to read precisely 26 characters, then read() may be better


Brilliant. Thanks!!




Please explain what this means. How is a 'variable' to be recognised?
And do you not in fact want to read exactly 26 characters after all?


What I meant was that it could keep reading string until it recognizes integer, nevermind, no idea if that's even (easily) possible.
Last edited on
Well, to recognise an integer, one way would be to read character-by-character and test each one to see whether it was a digit (or possibly a + or - sign which could be the first character of an integer). Though I'm sure that's not the only way.
A bit weird issue popped out with this...


When I'am outputting those values, each of them ends line before giving value.

With this code:

1
2
3
4
5
6
7
8
9
10
11
12
 

<...>
in.read(A[i].name,15);
A[i].name[15] = 0;
<...>

ofstream out("b.txt");
                   
for(i=0; i<P; i++) 
          
           {out << left << A[i].name << " " <<  B[i] }


in b.txt I get first line empty, second line: A[i] name, then B[i] integer value then it's automatically new line (notice that I don't have endl in my code yet it still ends it)

Last edited on
Managed to fix this by ending line before reading chars with in.ignore(90, '\n');, but can anyone explain please what this 90 actually means?
with no number it screws up, but seems that practically any number will do just as good as 90 :D
Last edited on
Topic archived. No new replies allowed.