3 Replies - 4930 Views - Last Post: 01 February 2011 - 01:21 PM Rate Topic: -----

#1 tekdaddy54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-September 08

Char Array to String conversion

Posted 01 February 2011 - 12:49 PM

I have an issue with the ability to convert a char array to a string. According to the Docs, the string.copy(char *,n, 0) will copy the array to the string value. When I tried it, it didn't work. I get blank for the sizet = buffer.copy(nwbuff,80,0) line. I can't find any reason why this shouldn't work. Please take a look.

 string extract(string ibuf){
    // do stuff here
    return ibuf;
 }

 string makcsv(string nbuf) {
    // stuff to strip and 
    // give back a CSV stream
    return nbuf;
 }

 int main() {
    int sp, sp1;
    size_t sizet;
    char *p;
    char fileName[80];
    string buffer;
    char nwbuff[80];
 
    cout >> "FileName: ";
    cin << fileName;

    // input file for parser
    ifstream fin(fileName);
    cout << "Here is the file contents: " <<endl;
    while (!fin.eof()) {
       fin.getline(nwbuff,80);
       sizet = buffer.copy(nwbuff,80,0);

    cout << "getl: " << nwbuff << endl;
    cout << "stri: " << buffer << endl;

 //   cout << extract(makcsv(buffer)) << endl;
    }
    return 0;
}



Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Char Array to String conversion

#2 vBx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-May 10

Re: Char Array to String conversion

Posted 01 February 2011 - 01:07 PM

std::string buffer(nwbuff) will work i guess.
edit:assuming the array is null terminated.

This post has been edited by vBx: 01 February 2011 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: Char Array to String conversion

Posted 01 February 2011 - 01:17 PM

while (!fin.eof()) {
       fin.getline(nwbuff,80);
       sizet = buffer.copy(nwbuff,80,0);

You're not reading into buffer, you're reading into nwbuff! buffer is EMPTY!
Was This Post Helpful? 0
  • +
  • -

#4 tekdaddy54  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-September 08

Re: Char Array to String conversion

Posted 01 February 2011 - 01:21 PM

You are correct. I discovered by mistake just after posting it( aaarrrrrgggg!) ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1