how to convert char to const char

This is a discussion on how to convert char to const char within the C++ Programming forums, part of the General Programming Boards category; I have a file which contains a year and the name of an associated file to be read. I need ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    8

    how to convert char to const char

    I have a file which contains a year and the name of an associated file to be read.
    I need to extract the data in the txt file and perform some calculations.
    ( year data file)
    2004 2004data.txt
    2005 2005data.txt
    2006 2006data.txt
    ...............................................
    Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata“ to ”const char yeardata”.

    Code:
    int oldnewcomp_temp(char* lcfile)
    {
           using namespace std;
     
           int year;
           char yeardata;
      
          ifstream inFile2009b;
           inFile2009b.open(lcfile); 
           inFile2009b >>  year >> yeardata  ;
           inFile2009b.close();
    
       ......
       
        ifstream yearlydata;
          yearlydata.open(yeardata);
       ......
         
           yearlydata.close();
      
     return 0;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    yeardata is just a single character you need a std::string.
    Then pass yeardata.c_str() to yearlydata.open(). Better still pass it to yearlidata's constructor.
    Kurt

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    also, this is clearly C++, and you posted in the C forum. C and C++ are not the same thing. they are two very distinct languages.
    Code:
    namespace life
    {
        const bool change = true;
    }

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    Thanks a lot, Zuk. I am quite new to c++ and I need further explanation. I tried to convert "char yeardata" to " std::string yeardata" and then "yearlydata.open(yeardata)" to "yeardata.c_str()" but it doesn't work ........
    Sorry for asking such a stupid thing.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,584
    Quote Originally Posted by china_108 View Post
    I tried to convert "char yeardata" to " std::string yeardata" and then "yearlydata.open(yeardata)" to "yeardata.c_str()" but it doesn't work
    explain how it doesn't work. "it doesn't work" is a very bad description of the problem, because it's impossible to help you with only that statement.

    Sorry for asking such a stupid thing.
    the only stupid question is the one that is not asked.
    Code:
    namespace life
    {
        const bool change = true;
    }

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    It is the same problem..... when I made the conversion, it says " cannot convert ‘std::string’ to ‘const char’ in initialization".

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Moved to C++ programming forum.

    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    Code:
    int oldnewcomp_temp(char* lcfile)
    
    {
    
           using namespace std;
    
      
           int year;
           std::string yeardata;
    
       
          ifstream inFile2009b;
           inFile2009b.open(lcfile); 
           inFile2009b >>  year >> yeardata  ;
           inFile2009b.close();
     
       ......
        
        std::ifstream yearlydata;
    
          yearlydata.open(yeardata.c_str());
    
       ......
          
           yearlydata.close();
       
     return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,916
    Look at the following snippet:
    Code:
           char yeardata;
       
     ...   
        ifstream yearlydata;
          yearlydata.open(yeardata);
    You defined yeardata as a single character, not a C-string. the ifstream.open() function requires a string, not a single character.

    Also there is no need to use the open() function, just open it with the constructor. And I also recommend you use a std::string instead of the C-string.
    Code:
        std::string yeardata;
    ...
    //    ifstream yearlydata(yeardata); // If you have a C++11 compliant compiler.
    //    ifstream yearlydata(yeardata.c_str()); // Otherwise.
    Jim

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    On which line does that error message refer to?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    Thanks to laserlight, Jim, Elkvis and Kurt, I got it right now.

    Thanks again!!!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    I suggest you avoid using char at all. They have a lot of gotchas and quirks which you must master in order to use them correctly and safely.
    So what's the solution, then? 99% of the times, it's std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    yes, I used std::string
    I forgot to add #include <string> at the beginning .......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert char* to const char*
    By nocturna_gr in forum C Programming
    Replies: 7
    Last Post: 12-18-2007, 07:59 AM
  2. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  3. Replies: 3
    Last Post: 12-06-2006, 06:59 PM
  4. Convert const char* to char*?
    By DmD in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 04:33 AM
  5. convert const char * to char*?
    By the Wookie in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2002, 12:23 AM

Tags for this Thread


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