Solved

convert char to a string in c++

Posted on 2003-10-29
10
16,138 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

19 Experts available now in Live!

Get 1:1 Help Now