Advertisement

07.01.2004 at 11:36AM PDT, ID: 21045142
[x]
Attachment Details

Converting a string to char array

Asked by girionis in C++ Programming Language

Tags: string, char, array

Hello I have the following code:

#include <iostream.h>
#include <fstream.h>
#include <string>
#include <iomanip>

int length(const char* str)
{
      int length;
      for (int i=0;str[i]!='\0'; i++)
      {
            length=i;
      }

      return length;
}

void break_lines(string buf)
{
      const char* str;
      strcpy(str, buf); <<<-------------------------- HERE

      int l = length(str);
      char buffer[l+1];
      for (int i=0, j=0;i<=l; i++)
      {
            if (str[i] != ' ')
            {
                  buffer[j] = str[i];
            }
            else
            {
                  cout << buffer << endl;
            }
      }
      
}

int readFile()
{
      ifstream inFile("rules.txt");

      string line;

      while (!inFile.eof())
      {
          getline(inFile, line);
            break_lines(line);
          cout<<line<<endl;
      }

      inFile.close();
      return 0;
}

int main()
{
      readFile();

      return 0;
}


I would like to convert the "buf" variable to "str" character array but I get the following error:

c:\applications\quincy99\assignment\start.cpp: In function `void break_lines(basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >)':
c:\applications\quincy99\assignment\start.cpp:20: passing `const char *' as argument 1 of `strcpy(char *, const char *)' discards qualifiers
c:\applications\quincy99\assignment\start.cpp:20: cannot convert `buf' from type `string' to type `const char *'

Any help of how I can do it is appreciated.

I am using the g++ compiler.

Regards

Start Free Trial
[+][-]07.01.2004 at 11:41AM PDT, ID: 11450295

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2004 at 11:42AM PDT, ID: 11450309

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: string, char, array
Sign Up Now!
Solution Provided By: jaime_olivares
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.01.2004 at 11:45AM PDT, ID: 11450333

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2004 at 11:48AM PDT, ID: 11450373

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2004 at 12:03PM PDT, ID: 11450505

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.01.2004 at 12:38PM PDT, ID: 11450856

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.01.2004 at 12:57PM PDT, ID: 11451014

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32