Solved

convert char to a string in c++

Posted on 2003-10-29
10
16,140 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mounting Linux Partition.... 30 347
Percona cluster member randomly crash 15 517
predeployment and postdeployment script in linux rpm 1 32
awk to variable in bash 2 41
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

786 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