Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[Question] C++ fstream ignore

Options
  • 03-11-2009 1:49am
    #1
    Registered Users Posts: 9,126 ✭✭✭


    Just a very simple problem here. I'm trying to use the ignore function of fstream in C++, but can't get it to work properly.
    ifstream inputFile;
    	inputFile.open(file);
    	inputFile.ignore(3);
    	inputFile >> alarmHrs;
    	inputFile >> alarmMins;
    	inputFile >> alarmSecs;
    

    The file is just a simple text file that reads
    0
    0
    0
    7
    45
    0
    

    Basically the first three values are used in a superclass constructor, so I want to just ignore them. The code above only ignores the first value, so the variables get set to 0,0,7, instead of 7,45,0. Even writing inputFile.ignore() three times has the same effect.

    I know it's very simple but I've been playing with this for at least 20 minutes now and can't get it to work, found nothing on google.


Comments

  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    Never used it, but looking at the docs, it says it ignores n characters, not tokens. There are newlines in that text?


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    That file would actually be:
    0\r\n
    0\r\n
    0\r\n
    7\r\n
    45\r\n
    0
    

    Where '\r' and '\n' are carriage return and newline characters.

    Perhaps without the '\r' depending on what OS and text editor used to create it, but you need to take those extra chars into account.


  • Registered Users Posts: 9,126 ✭✭✭Royale with Cheese


    Yeah cheers, I changed it to 9 and it works grand.

    Have to hand it in and it'll be tested using a different text file, so I guess there's a chance there won't be \r in it? I'll just stick it in the comments, should be grand.


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    You could do something like:
    	ifstream inputFile;
    	inputFile.open(file);
    	for (int i=0; i<4 ; i++)
    		inputFile >> alarmHrs;
    	inputFile >> alarmMins;
    	inputFile >> alarmSecs;
    
    Which'd work no matter whether newline was denoted by "\r\n" or just "\n".

    That's not very elegant though, I don't know what the standard way to do something like this would be.


  • Registered Users Posts: 9,126 ✭✭✭Royale with Cheese


    You could do something like:
    	ifstream inputFile;
    	inputFile.open(file);
    	for (int i=0; i<4 ; i++)
    		inputFile >> alarmHrs;
    	inputFile >> alarmMins;
    	inputFile >> alarmSecs;
    
    Which'd work no matter whether newline was denoted by "\r\n" or just "\n".

    That's not very elegant though, I don't know what the standard way to do something like this would be.

    That is exactly what I had, didn't really like the look of it.


  • Advertisement
Advertisement