char[255] to string?

This is a discussion on char[255] to string? within the C++ Programming forums, part of the General Programming Boards category; How would I do something like this: Code: char en_file1[255]; int file1_len; ifstream getFileName ("aFile.ini"); getFileName.getline(en_file1,255,'\n'); file1_len = strlen(en_file1); for ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    char[255] to string?

    How would I do something like this:

    Code:
    char en_file1[255];
    int file1_len;
    
    ifstream getFileName ("aFile.ini");
    
    getFileName.getline(en_file1,255,'\n');
    
    file1_len = strlen(en_file1);
    
    for (int i = 0; i < file1_len; i++)
    {
    	de_file1[i] = en_file1[i];
    }
    This compiles without any errors/warnings BUT when I run it, it says the program has had an error and will now close...

    basically I need somthing that will convert a character[255] to a string.
    ~guitarist809~

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    There seems to be no reason at all for you to even worry about this. Just use a std::string and forget about it, don't even convert.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    well, I think i forgot to mention some things.

    1. I am using this program to decrypt/encrypt files...
    2. getFileName.getline... requires a character array to work, strings return an error while compiling
    3. i wrote a function that encrypts/decrypts these files but only uses strings [encrypt(string, string string&);]

    im in quite a bit of a issue here
    ~guitarist809~

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    There should be an implicit conversion if you pass a char array to a function that expects a std::string. That said, you can use the non-member getline function instead:
    Code:
    std::string en_file1;
    getline(getFileName,en_file1,'\n');
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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