[try Beta version]
Not logged in

 
how to assign characters with empty spaces into single string

Jun 17, 2013 at 2:22pm
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 Jun 17, 2013 at 2:53pm
Jun 17, 2013 at 2:24pm
std::string myString = "potatoe onion tomatoe 1234";

Edit: Bonus points for the Quaylism :)
Last edited on Jun 17, 2013 at 2:24pm
Jun 17, 2013 at 2:31pm
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 Jun 17, 2013 at 2:41pm
Jun 17, 2013 at 2:42pm
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)
Jun 17, 2013 at 2:49pm
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.
Jun 17, 2013 at 2:49pm
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 Jun 17, 2013 at 2:54pm
Jun 17, 2013 at 2:52pm
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;


Jun 17, 2013 at 3:23pm
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 Jun 17, 2013 at 3:24pm
Jun 17, 2013 at 4:23pm
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 Jun 17, 2013 at 4:26pm
Jun 17, 2013 at 4:33pm
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.
Jun 17, 2013 at 7:13pm
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 Jun 17, 2013 at 7:17pm
Jun 17, 2013 at 7:32pm
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 Jun 17, 2013 at 7:34pm
Topic archived. No new replies allowed.