Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


scanf questions

This topic is 6120 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

Hiya I have a roblem concerning scanf that you might be able to help me with. Example lines from text file (hope this formats right):
859613     3 Feb    4 Feb   10 Feb   11 Feb   17 Feb   18 Feb   24 Feb   25 Feb   14 Feb          
  840683     3 Feb    4 Feb   10 Feb   11 Feb   17 Feb   18 Feb   24 Feb   25 Feb    2 Feb    9 Feb 
heres the code ive been using so far:
sscanf(buffer,	"  %*li%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi%*[A-Za-z ]%hi",
buffer contains 1 line from the sample data above. Ugly eh ? Anyways, what i need is to get the dates into an array. As you can see i dont know for sure how many dates are given, only that there will be no more than 10. 2 Questions. 1) This cant be the easiest way to do this right ? what are my options ? 2) My main problem is that when 10 dates are not given the empty array indexes wont be initialized correctly (gets filled with junk). How can i avoid this? Its getting late and im not thinking straight anymore, so please forgive my (probably) stupid questions. I need to get this done though, so any help will be very appreciated. Thanks

Share this post

Link to post
Share on other sites
Why not use fscanf to scan from a file? I think it will probably be easier. I''ve never used sscanf before so my little snippet is pretty basic.

Well I think I can tell you what''s happening. It looks like your format string has tokens in it that aren''t in your buffer. I also don''t know what %hi or %li is. I''ve never seen those.

Here''s an exmaple that should work:

int main(void) {
char buffer[100] = "The girl has 10 brown mice.";
char adjective[10] = {0};
int number = 0;

sscanf(buffer,"The girl has %d %s mice.",&number,adjective);
printf("The girl has %d %s mice.",number,adjective);

Hope this helps

Share this post

Link to post
Share on other sites
Thanks, even though it doesnt answer my question

I dont think i made myself entirely clear though.

First, %hi and %li are short ints and long ints respectively (to avoid unnescessary casts). The %*[A-Za-z ] scansets are used to discard months and white-spaces since i dont need those.

I know about fscanf, and was planning to switch to it when i got the other working. When it comes to tokens, the syntax is the same for both functions, which is why it really doesnt make any difference which function i use.

Anyway, my main problem persists. How do i avoid the empty array indexes to be filled with garbage ? Or how can i tell whether or not they were filled correctly ?

Please keep the input coming.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!