Thread: how to convert char to const char

  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
    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
    3,445
    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.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  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
    3,445
    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.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  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
    28,413
    Moved to C++ programming forum.

    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    4,632
    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
    28,413
    On which line does that error message refer to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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-14-2002, 11:23 PM

Tags for this Thread