Thread: invalid const char[] and char [] to binary operator+

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    invalid const char[] and char [] to binary operator+

    hi to all!
    i'm posting for the first time here and also i'm new to c++! So,excuse me a priori. I'm trying to open a file with ifstream like this:

    Code:
    std::ifstream      file("/home/alex/Desktop/csvDumps/"+event->name);
    where 'event' is an inotify_event struct and 'name' is defined as char name[]; in this struct.

    but i get : "error: invalid operands of types ‘const char [29]’ and ‘char [0]’ to binary ‘operator+’".

    Tried to cast event->name by:
    Code:
    std::ifstream      file("/home/alex/Desktop/csvDumps/"+(const char)event->name);
    but nothing went right.

    Please help!
    This code is in a while(1) loop and every time the name of the file to be opened changes. Any other idea for opening the file with an input variable like the one before is accepted!!

    thanks in advance!!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    unlike java and C#, you cannot add strings using the + operator in C++ the way you are trying to do it in this code. a string is not a distinct builtin type in C++. if you change the declaration of inotify_event::name to an std::string, then you can use the + operator, but unless you have a C++ compiler that supports the new standard (C++11), you cannot use an std::string as the filename parameter to std::fstream:pen. it must be a const char*.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You cannot concatenate two C-strings using operator+. You either need a buffer and std::strcat - cppreference.com, or better:

    Code:
    std::ifstream file((std::string("/home/alex/Desktop/csvDumps/") + event->name).c_str());
    If your compiler supports C++11:
    Code:
    std::ifstream file(std::string("/home/alex/Desktop/csvDumps/") + event->name); // std::string is accepted

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Quote Originally Posted by kmdv View Post
    You cannot concatenate two C-strings using operator+. You either need a buffer and std::strcat - cppreference.com, or better:

    Code:
    std::ifstream file((std::string("/home/alex/Desktop/csvDumps/") + event->name).c_str());
    If your compiler supports C++11:
    Code:
    std::ifstream file(std::string("/home/alex/Desktop/csvDumps/") + event->name); // std::string is accepted

    thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  2. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  3. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 12:58 PM
  4. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  5. invalid conversion from 'char' to 'const char*'
    By LiKWiD in forum C++ Programming
    Replies: 10
    Last Post: 03-20-2005, 03:50 AM

Tags for this Thread