#### Archived

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

# scanf questions

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

## 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 779720 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", &Crew->crewList[i].Bids[0], &Crew->crewList[i].Bids[1], &Crew->crewList[i].Bids[2], &Crew->crewList[i].Bids[3], &Crew->crewList[i].Bids[4], &Crew->crewList[i].Bids[5], &Crew->crewList[i].Bids[6], &Crew->crewList[i].Bids[7], &Crew->crewList[i].Bids[8], &Crew->crewList[i].Bids[9]); 
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 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:
#include

int main(void) {
char buffer[100] = "The girl has 10 brown mice.";
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 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 ?