Thread: remove first character from line

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    remove first character from line

    How would I remove the first character from each line of a file?
    I put each line into temp and try to change the first character but it tells me that I can't convert from "const char" to "char".

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
        ifstream file;
        ofstream out;
    
        file.open("first.txt");
        out.open("temp.txt");
    
        string temp;
    
        while(file.good())
        {
            getline(file, temp);
    
            temp[0] = ""; //doesn't work
            out << temp << endl;
        }
    
        out.close();
        file.close();
    
        cin.get();
        return 0;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use the string::erase() member function.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    thanks.

    it seems to work now after changing
    Code:
    temp = "";
    to

    Code:
    temp.erase(0,1);

  4. #4
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    Code:
    temp[0] = '\0';
    This will produce no syntax errors.

    Anything contained within "quotes" can be considered as a char* pointer, even if there's a single character in it. So, by putting temp[0] = ""; you basically tried to assign a char* pointer to something that expected a character.

    You should use the functions already described to you, if they do the trick, but I believe it's even more important to understand why the compiler is complaining.

    Try this code for an example:
    cout << (int) "a" << endl;
    cout << (int) 'a' << endl;
    cout << (int) "" << endl;
    cout << (int) '\0' << endl;

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by Mr.Pointer View Post
    Code:
    temp[0] = '\0';
    This will produce no syntax errors.

    Anything contained within "quotes" can be considered as a char* pointer, even if there's a single character in it. So, by putting temp[0] = ""; you basically tried to assign a char* pointer to something that expected a character.

    You should use the functions already described to you, if they do the trick, but I believe it's even more important to understand why the compiler is complaining.

    Try this code for an example:
    cout << (int) "a" << endl;
    cout << (int) 'a' << endl;
    cout << (int) "" << endl;
    cout << (int) '\0' << endl
    ;
    will these actually work in C++? i thought (int) var stuff belongs to C! and type casting in C++ differs actually!
    Code:
     static_cast<int> (sth)
    i'd be very thankful if you or anyone else can make it clearer .
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Pointer
    This will produce no syntax errors.
    It will also do the wrong thing since temp is a std::string object. Actually, even if temp was a null terminated string, assigning a null character to the first character will just truncate the string to zero length instead of removing the first character. The solution that not_someguy eventually used is correct.

    Quote Originally Posted by Mr.Pointer
    Anything contained within "quotes" can be considered as a char* pointer, even if there's a single character in it. So, by putting temp[0] = ""; you basically tried to assign a char* pointer to something that expected a character.
    Loosely speaking, that is true, but technically "" is a const char[1], not a char*.

    Quote Originally Posted by Masterx
    will these actually work in C++? i thought (int) var stuff belongs to C! and type casting in C++ differs actually!
    I recommend the use of the appropriate cast like static_cast in C++, but otherwise the C-style casts still work in C++.
    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

  7. #7
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    It will also do the wrong thing since temp is a std::string object
    Yeah, like I said, he should use the functions suggested to him. I just thought it would be very important to understand just why it produced a syntax error.

    Loosely speaking, that is true, but technically "" is a const char[1], not a char*.
    Not that it changes anything in the long run, but thanks for the clarification

    I recommend the use of the appropriate cast like static_cast in C++, but otherwise the C-style casts still work in C++.
    I only recently migrated to C++ after having a basic knowledge of C, and I'm still using C-style casting. Thank you for your suggestion, I will look into it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. notepad doesn't recognize new line character
    By sp2 in forum C Programming
    Replies: 11
    Last Post: 04-30-2008, 10:55 AM
  2. Remove last character if asterix?
    By manny in forum C Programming
    Replies: 5
    Last Post: 08-22-2007, 12:09 PM
  3. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Output text containing '\r' as new line character.
    By anonytmouse in forum C Programming
    Replies: 4
    Last Post: 11-17-2003, 08:47 PM