Solved

convert char to a string in c++

Posted on 2003-10-29
10
16,139 Views
Last Modified: 2011-08-18
here is what i am using (reading in a file)

ifstream OpenFile("test.file");
    char ch;
    while(!OpenFile.eof())
    {
      OpenFile.get(ch);
      //cout << ch;
           }
OpenFile.close();

the out-put is stored in "ch" right ...ok i need to convert that to a string
0
Comment
Question by:nutter01
10 Comments
 

Expert Comment

by:milanygandhi
ID: 9640884
You can include following statements after the comment //cout<<ch

char str[2];
str[0]=ch;
str[1]='\0'; //Null character

str is the string you want.

0
 
LVL 3

Expert Comment

by:_tack
ID: 9640924
nutter,

with your program you read one char at a time, is that what you relly want ?

0
 

Author Comment

by:nutter01
ID: 9641099
ok sorry guys my fault i forgot to mention this ....that "test.file" contains a username and a password on the first line in this format     username:password   .....i was just using the OpenFile to read the first line which it appeared to do because if i did cout << ch << endl; it printed that whole username:password fine. The reason for needed it converting to a string is because i have to encrypt it with base64 and my base64 class requires a string not a "char". Anyway the above example didn't work but's that my fault for not explaining myself the first time. I am more than open for a better way to read the first line of that test.file. Once again sorry for the lack of info
0
 
LVL 3

Expert Comment

by:_tack
ID: 9641165
This is not the best way of doing it, but it should work.

The best would be to read the line, then look for ':' in the line and split the line in 2 substrings
in C strtok will do it.

string username;
string password;
int colonfound=0;

ifstream OpenFile("test.file");
    char ch;
    while(!OpenFile.eof())
    {
       OpenFile.get(ch);
       if(ch != ':')
       {
         colonfound=1;
       }
       else if(colonfound == 0)
         username+=ch;
       else
         password+=ch;
     }
OpenFile.close();
0
 

Author Comment

by:nutter01
ID: 9641241
**** sorry mate i have had another shocker! ...that username:password ....i need to keep it in that exact format when i pass it to the base64 for encrytping so no need to split the 2, i even need the colon in there. Just from your example above what converts the char (ch) to the string?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
_tack earned 125 total points
ID: 9641282
just do

string read_line;

ifstream OpenFile("test.file");
    char ch;
    while(!OpenFile.eof())
    {
       OpenFile.get(ch);
         read_line+=ch;
     }
OpenFile.close();

0
 
LVL 3

Expert Comment

by:_tack
ID: 9641291
of course, you might also want to ignore '\n' and '\r' chars, just in case

if ( ch=='\r' || ch=='\n')
  continue; // or break -- continue will read next line too, break will exit the loop
0
 

Author Comment

by:nutter01
ID: 9641401
thanks mate that works great :-)  .... that line "read_line+=ch; is that the one that converts to a string?
0
 
LVL 3

Expert Comment

by:_tack
ID: 9641430
read_line += ch
is an append, in this case you append one char to your existing string.

is the same as
read_line = read_line + ch;
0
 

Expert Comment

by:amir_0895
ID: 10153630
use STL:
#include <string>

{
   string string_ch(ch);
}

and there you have it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Debian 3 - initrd rebuild 3 69
how to run a command as a nologin user 7 311
Embeded Linux on Router 9 98
BASH script to modify crontab? 3 47
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now