Thread: Quick problem aboutf scanf

  1. #1
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24

    Quick problem about fscanf

    Hey guys,

    I'm trying to scan a file that contains:
    Code:
    john smithenson #John
    andrea johnson #Andrea
    michael smith #Michael
    And Here is my code...I want to store the names into an array...the last name in a different array...and the comment for the name in another array. All 3 different arrays.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    #ifndef BUFSIZE
    #   define BUFSIZE	256
    #endif
    
    #ifndef TAGLEN
    #   define TAGLEN	28
    #endif
    
    #ifndef NTAGS
    #   define NTAGS	64
    #endif
    
    #ifndef CONFIGFILE
    #   define CONFIGFILE  "config.txt"
    #endif
    
    int main(void) {
    	
    	FILE *configFile;
    	char first[NTAGS][TAGLEN];
    	char last[NTAGS][TAGLEN];
    	char extra[BUFSIZE];
    	char buffer[BUFSIZE];
    
    	int i = 0, n = 0, spaceCount = 0;
    
    	if((configFile = fopen(CONFIGFILE, "rb")) == 0) {
    		perror("fopen\n");
    		exit(1);	
    	}	
    
    	while ( fgets(buffer, sizeof(buffer), configFile) != NULL ) {
    		for(i = 0; buffer[i] != '\0'; i++) {
    			buffer[i] = tolower(buffer[i]);
    			if(isspace(buffer[i])) {
    				spaceCount++;	
    			}
    		}
    		if(spaceCount == 2) {
    			fscanf(configFile, "%s %s %s", first[n++], &last[n++], &extra[n++]);
    		}
    	}
    	
    	fclose(configFile);
    	return 0;
    }
    I'm trying to make it so if there is 2 spaces per line...it will scan the line...else it will just ignore the line.

    Any help would be greatly appreciated!

    Kind regards,
    JB
    Last edited by BuezaWebDev; 03-28-2005 at 06:44 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. extra needs to be a 2D array, just like first and last
    2. You increment n 3 times in your scanf - this is very bad

    Code:
    fscanf(configFile, "%s %s %s", first[n], last[n], extra[n]);
    n++;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24
    Ah, thank you! It worked!

    But for some reason it's not reading the whole config file. My first post had different code than what I have now:

    In my config file:
    Code:
    italic
    bold 0;31 # red
    iTAlic 0;34 # this is the line in effect for italic
    underline 0;32 # green
    italic 0;35 # overriden by line 3
    In my code:
    Code:
    int main(void) {
    	
    	FILE *configFile;
    	FILE *htmlFile;
    	char possibleTags[NTAGS][TAGLEN];
    	char ascii[BUFSIZE][BUFSIZE];
    	char extra[BUFSIZE][BUFSIZE];
    	char buffer[BUFSIZE];
    
    	int i = 0, n = 0, spaceCount = 0;
    
    	/*******************************************
    	** READ CONFIG FILE FOR POSSIBLE TAGS
    	********************************************/
    	if((configFile = fopen(CONFIGFILE, "rb")) == 0) {
    		perror("fopen\n");
    		exit(1);	
    	}	
    
    	while ( fgets(buffer, sizeof(buffer), configFile) != NULL ) {
    		for(i = 0; buffer[i] != '\0'; i++) {
    			buffer[i] = tolower(buffer[i]);
    			if(isspace(buffer[i])) {
    				spaceCount++;	
    			}
    		}
    		if(spaceCount >= 2) {
    			fscanf(configFile, "%s %s %s", possibleTags[n], ascii[n], extra[n]);
    			n++;
    		}
    	}
    	fprintf(stdout, possibleTags);
    	fclose(configFile);
            return 0;
    }
    When I use the fprintf to output the array, it only displays "bold" as one of the tags it scanned.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well now, you need some advanced sscanf usage

    Code:
    fscanf(configFile, "%s %s %[^\n]", possibleTags[n], ascii[n], extra[n]);
    The last one is a scan set (actually an inverted scan set). It's like %s, but you get to choose which chars are considered part of the string, and which are not. %s being a special case which basically amounts to "%[^ \t\r\n]"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24
    Interesting, would it be possible to validate the input as well? Like...what I'm trying to do here:

    Code:
    	while ( fgets(buffer, sizeof(buffer), configFile) != NULL ) {
    		for(i = 0; buffer[i] != '\0'; i++) {
    			buffer[i] = tolower(buffer[i]);
    			if(isspace(buffer[i])) {
    				spaceCount++;	
    			}
    		}
    		if(spaceCount >= 2) {
    			fscanf(configFile, "%s %s %[^\n]", possibleTags[n], ascii[n], extra[n]);
    			if(ascii[n] == '\0') {
    				continue;	
    			}
    			n++;
    		}
    	}
    I want it to check if the ASCII code is null, and if it is...then it will skip the tag and go to the next one for input. If the ASCII code isn't null, then it'll add that ..input of the fscanf into the array...or am I getting the wrong logic??

    I still keep getting an output of :

    Code:
    bold
    
    inputs of my htmlfile here
    So yeah, it keeps reading only 1 tag.

    This is what I have in my config file too:
    Code:
    italic
    bold 0;31 # red
    iTAlic 0;34 # this is the line in effect for italic
    underline 0;32 # green
    italic 0;35 # overriden by line 3
    It skips the first line...because it doesn't have enough spaces (which is good and exactly what I want), and then it reads "bold" but it doesn't read the next few lines...and I have no idea why

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By transgalactic2 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 09:43 PM
  2. just a quick problem
    By Domdom in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2006, 11:11 AM
  3. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. Hi I have a small problem!! Need Quick Help
    By simly01 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2002, 01:14 AM