Solved

convert char to a string in c++

Posted on 2003-10-29
10
16,143 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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