File reading code - beyond help.

This is a discussion on File reading code - beyond help. within the C Programming forums, part of the General Programming Boards category; I've been trying to write some code to read a file in this sort of format Code: #comment blah blah ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    Unhappy File reading code - beyond help.

    I've been trying to write some code to read a file in this sort of format

    Code:
    #comment blah blah
    
    [catagory]
    variable=value
    
    [another catagory]
    variable=value
    variable=value
    But I got lost and ended up with this useless piece of ****:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define TEST_FILE "test.dat"
    
    
    int caseConvert(char *str)
    {
        char a;
    	while(*str)
    	{
    		*str=tolower(*str);
    		*str++;
    	}
    	return 0;
    }
    
    int firstword(char *inputstring, char *output)
    {
    	int i = 0;
    	int j = 0;
    	int k = 0;
    	while(*inputstring != '=')
    	{
    		i++;
    		*output = *inputstring;
    		*output++;
    		*inputstring++;
    	}
    	return i + 1;
    }
    
    int getEntry(char *section,char *entryname, char *filename)
    {
    	FILE *datafile = fopen(filename,"r");
    	int flag = 0;
    	char line[100];
    	char secname[BUFSIZ];
    	char currentword[BUFSIZ];
    	char thingy[BUFSIZ];
    	caseConvert(section);
    	caseConvert(entryname);
    	int i = 0;
    	int j = 0;
    	int k = 0;
    
    	int returnNumber=0;
    
    	if(!datafile)
    	{
    		return -1;
    	}
    
    	while(fgets(line, 100, datafile)!=NULL && flag != 1)
    	{
    		j = firstword(line, currentword);
         	if(line[0] == '#') continue;
    	 	else if(line[0] == '[')
    	 	{
    		 	do
    		 	{
    			 	i++;
    			 	secname[i-1] = line[i];
    		 	}
    		 	while(line[i+1] != ']');
    		}
    		else if(strcmp(currentword,entryname) && strcmp(section,secname))
    		{
    			printf("%c",line[j]);
    			while(line[j])
    			{
    				thingy[k] = line[j];
    				j++;
    				k++;
    			}
    		}
    	}
    	return 0;
    }
    
    
    int main(void)
    {
    	int i = 0;
    	i = getEntry("other","percent", TEST_FILE);
    	printf("%d",i);
    	return 0;
    }
    If anyone could fix my hopeless code, I'd be very greatful

    here's the file it needs to read:
    Code:
    #test.dat
    [other]
    percent=1
    #'s are ignored lines

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd use 'fgets' and read a line at a time into a buffer, then check the first character of the line to see what to do with it. A 'switch' would do good here.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Here is something to try. Hope you don't mind the rewrite, but there were too many errors with the old code. Let me know how it works. You might need to modify it slightly, as I wasn't sure exactly how it might be used.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define TEST_FILE "test.dat"
    
    
    int caseConvert(char *str)
    {
    	while(*str)
    	{
    		*str=tolower(*str);
    		str++;
    	}
    	return 0;
    }
    
    
    int getEntry(char *section,char *entryname, char *filename)
    {
    #define LINE_SIZE 100
    #define FIND_ENTRY 1
    #define FIND_SECTION 2
    #define	TRUE 1
    #define FALSE !TRUE
    
    	FILE *datafile;
    	char line[LINE_SIZE];
    	char secname[BUFSIZ];
    	char curentry[BUFSIZ];
    	int action;
    	int done = FALSE;
    	int i = 0;
    	int retval = 0;
    
    
    	if((datafile = fopen(filename,"r")) != NULL)
    	{
    		action = FIND_SECTION;
    
    		while(fgets(line, LINE_SIZE, datafile)!=NULL && !done)
    		{
    			if(line[0] != '#' && line[0] != '\n') 
    			{
    				switch(action)
    				{
    				case FIND_SECTION:			
         	 			if(line[0] == '[')
    	 				{
    		 				do
    		 				{
    			 				i++;
    			 				secname[i-1] = line[i];
    		 				}
    		 				while(line[i+1] != ']');
    
    						secname[i] = '\0';
    
    						caseConvert(secname);
    
    						if (!strcmp(section, secname))
    						{
    							printf("found the correct section: %s\n", secname);
    							action = FIND_ENTRY;
    						}
    					}
    					break;
    
    				case FIND_ENTRY:
    					// start reading the entries here
    					
    					i = 0;
    
    					if (line[i] == '[') // if we read this character, we are at a different section
    						done = TRUE;
    					else
    					{
    						while (line[i] != '=' && line[i])
    		 				{
    			 				curentry[i] = line[i];
    							i++;
    		 				}
    		 				curentry[i] = '\0';	
    						
    						caseConvert(curentry);
    
    						printf("%s\n", curentry);
    					}
    					break;
    				}
    			}
    		}
    	}
    	else
    		retval = -1;
    
    	return (retval);
    }
    
    
    int main(void)
    {
    	int i = 0;
    	char section[21] = "other";
    	char entryname[21] = "percent";
    
    	i = getEntry(section,entryname, TEST_FILE);
    	printf("%d",i);
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    thanks that works
    as long as the first section is the section you're looking for.

    I'm trying to fix that but it's not going too well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 8
    Last Post: 03-26-2007, 05:48 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21