Sign in to follow this  

Input from file issues (c++)

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to read in input from a file listed below: L ( 10, 10) (10, 60) 255 0 0 using this code:
[source lang=c++]

ins >> current;
if (current == 'L') // Line
{
/********************* POINT 1 ***************/
  ins >> current; // get rid of (
  ins >> currentVal; // take in Value
  newPoint1.setX(currentVal); // Set first point X


The ifstream ins has been initialized. When I take in current in line 1, the debug says it is L, which is correct. I do it again to get rid of the (, which also works. However, when I write ins >> currentVal, which was initialized as an integer, it just skips over it, not changing the value of currentVal, which was initialized to 0. The if statement is entered, because the first value correctly works. Why is it skipping over the int? If I change int currentVal to float currentVal, that works too, but I need to send setX an integer. What am I missing?

Share this post


Link to post
Share on other sites
Quote:
Original post by UMichGOBLUE
how would I use getline () to do that?




// Input buffer
char szBuffer[101];

// Get one line into the buffer. 100 characters maximum.
ins.getline( szBuffer, 100 );


Share this post


Link to post
Share on other sites
I'm sorry I'm really not familiar with getLine. How would I access the information that I need? Also, where I have the first line starting with L, there could be many lines after that in which I need to do the same thing. Is there any way you could kind of lay it out for me because I have no clue. Thanks for all your help.

Share this post


Link to post
Share on other sites
The problem your having is that the >> operator is being very greedy when it comes to the stream. It's gobbling up ',' along with the digit and trying to perform a conversion on it and its probably not converting to a number which is causing the return value to not be updated. If you format your string with spaces in it:
L( 10 , 10 )
then int will work. The only reason the other operators are probably working is because there implementations are ignoring things that are definetly characters where as int is trying to convert the characters to there numerical values.

PS: strtok is a tokenizer that will allow you to break a string up into sections. So you can use getline to grab like 100 characters then build a tokenizer (its an input stream with built in parsing) that will feed you the sections of the string stopping at whatever point you want. So you could have it break at the '(' and the ',' for example. Just look up the man on strtok and it should be explained.

Share this post


Link to post
Share on other sites
Whatever you do DON'T USE STRTOK. The function creates far more problems that it solves.


  • It has a weird syntax (passing a buffer once and NULL subsequently)

  • It is often implemented with a static buffer and thus cannot be used with threading or multiple buffer simultaneously

  • It encourages the use of C-strings in C++ which is never a good idea

Share this post


Link to post
Share on other sites
Is this a homework assignment? If so, you might have to go the "icky" route.

If not, just change the format of your string to be something like this:

L ( 10 10 ) ( 10 60 ) 255 0 0

RIZAX is right. It's choking on "10,", because it's trying to read an int and finding something it doesn't understand. Modifying the suspect line like above is the easiest solution.

But if you can't do that, odds are you'll have to read the whole line in and break it up according to delimiters. Which seems like it might be a tiny bit over your head at this point. In any case, I agree with cshowe - it'd be best to avoid strtok in lieu of better alternatives.

Share this post


Link to post
Share on other sites

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this