Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

[Question] C++ fstream ignore

  • 03-11-2009 01:49AM
    #1
    Registered Users, Registered Users 2 Posts: 9,394 ✭✭✭


    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, Registered Users 2 Posts: 9,394 ✭✭✭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, Registered Users 2 Posts: 9,394 ✭✭✭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