Thread: converting string into char*

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    converting string into char*

    Hi,

    I am having some problems. I started with c++ and so far am finding it a bit confusing. (But i'll get there). the problem i am having right now is, how to convert string to char* . I was told the memcpy is the fastest way but i cannot figure out why it is not working. Oh and is there some tutorial regarding string manipulations in c++.


    Code:
    #include <cstring>
    #include <vector>
    #include <fstream>
    #include <iostream>
    
    
    // Read a file into a vector
    
    void help(char *program) {
      std::cout << program; 
      std::cout << ": Need a filename for a parameter.\n";
    }
    
    int main(int argc, char* argv[])
    {
      if (argc < 2){ 
    	help(argv[0]);
    	return 1;
      }
      
      std::vector<std::string> file;
      std::string line;
      
      file.clear();
      std::ifstream infile (argv[1], std::ios_base::in);
    
      int size = 0;
      while (getline(infile, line, '\n')){
        file.push_back (line);
        size += line.length();
      }
    
      char *a;
      a = new char[size];
      for (int i = 0; i<file.size();i++){
    	  memcpy(a[i+file[i].length()],file[i],file[i].length());
      }
    
      return 0;
    }
    so i need the lines i read from file to be in a char array since some other c function require it like that. Is there any better way to do this ??

    thank you


    P.S.
    Here is the error:

    Code:
     cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘const void*’ for argument ‘2’ to ‘void* memcpy(void*, const void*, size_t)’
    baxy
    Last edited by baxy; 11-28-2012 at 08:36 AM. Reason: error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string to char array
    By frankchester in forum C Programming
    Replies: 13
    Last Post: 12-01-2010, 01:33 AM
  2. Converting string to char
    By aama100 in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2008, 06:23 AM
  3. string to char converting problem
    By bergziege in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 03:37 PM
  4. converting char to string
    By deleeuw in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2003, 08:01 PM
  5. problems converting string to char[]
    By Jan79 in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2003, 03:28 AM