Solved

convert char to a string in c++

Posted on 2003-10-29
10
16,142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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