Thread: Reading integers in sscanf

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Reading integers in sscanf

    So I'm trying to read a text file and at the start of each line is a timestamp, for example

    02:04 xxxxxxxxxxxxxxxxxxxxxx

    This is the function that I've created,

    Code:
    int check_clicks(char *str, char heroes[10][20], char players[10][20], char hero_names[10][20]) {
    	char getLine[1024];
        FILE *fp;
        int i,j,ID,minutes,seconds,count=0;
    	char player[20],name[20],hex[10],hero[20];
    	fp = fopen(str, "r");
        if(fp == NULL) {
            printf("Missing log file\n");
            exit(20);
        }
    	while(fgets(getLine, 1024, fp) != NULL) {
    		if(sscanf(getLine, "%d:%2d %s %s %s %*s %*s %*s %s",&minutes,&seconds,player,name,hex,hero)==6) {
    			if((minutes>=2)&&(seconds>=15)) {
    				break;
    			}
    			if((strcmp(player, "Player:")==0)&&(strcmp(hex,"0x19:")==0)) {
    				for(j=0;j<10;j++) {
    					if(strcmp(players[j],name)==0) {
    						ID = j;
    					}
    				}
    				for(i=0;i<10;i++) {
    					if(strcmp(heroes[i],hero)==0) {
    						if((ID<5)&&(i>4)) {
    							printf("%s(%s) clicked on %s(%s) at 0%d:%2d\n",players[ID],hero_names[ID],players[i],hero_names[i],minutes,seconds);
    							count += 1;
    						}
    						else if((ID>4)&&(i<5)) {
    							printf("%s(%s) clicked on %s(%s) at 0%d:%2d\n",players[ID],hero_names[ID],players[i],hero_names[i],minutes,seconds);
    							count += 1;
    						}
    					}
    				}
    			}
    		}
    	}
    	return count;
    }
    Now when i test my program out whenever there is a 0 it just produces whitespace.

    Eg.

    R3D_Drag0n(Shadow Fiend) clicked on CMW_(Storm Spirit) at 01: 8

    Now as I'm only looking at the first 2 minutes and 15 seconds, I have manually put a 0 in front of the minutes part, however %2d does not produce "08" but " 8" as you can see.

    Any help much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should read up on format specifiers to printf, particularly the difference between %2 and %02.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Reading from binary file; fread problems?
    By pmgeahan in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 05:07 PM
  3. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  4. fread - reading strings and integers.
    By Vber in forum C Programming
    Replies: 1
    Last Post: 11-17-2002, 04:08 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM