char conversion

This is a discussion on char conversion within the C++ Programming forums, part of the General Programming Boards category; how would i convert a char array, ie. address[256] into a string?...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    char conversion

    how would i convert a char array, ie. address[256] into a string?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Code:
    #include <string>
    #include <iostream>
    #include <ostream>
    int main()
    {
    	char chars [] = "Did you try?";
    	std::string example;
    	example = chars;
    	std::cout << example.c_str() << std::endl;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    char charAddress[] = "123 Maple Way";
    string strAddress = charAddress;
    cout<<strAddress<<endl;

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    that would convert a char array of size 256 where array[i] is an element to a string, ie. say i have an address which is made up of 20 chars will that convert it to a string?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    If the data in the character array is properly NULL terminated, then you can simply assign the character array to the string container object and it will be converted (as has been demonstrated). If the first 20 characters out of that 256 length array contain data (with the 21st character being a NULL), then the resulting string will contain 20 characters.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> std::cout << example.c_str() << std::endl;

    No need for c_str() there.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    say for example i have a text file, and im using I/O stream, my file is constantly being update so i dont know how many lines there will be, is there a way of getting the num of lines?

  8. #8
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    No need for c_str() there.
    I know, just showing how you would go about converting a string back to a c style char array

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> is there a way of getting the num of lines?

    You would have to make a pass through the file all the way through to count the number of lines, then go back to the beginning to read it again. However, if you use C++ tools like vector and string then you rarely need to know the exact number of lines because those things grow automatically and you can just loop through the file one line at a time until you reach the end. This would be the preferred approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  2. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 10:17 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21