I've been trying to write some code to read a file in this sort of format
But I got lost and ended up with this useless piece of ****:Code:#comment blah blah [catagory] variable=value [another catagory] variable=value variable=value
If anyone could fix my hopeless code, I'd be very greatfulCode:#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; }
here's the file it needs to read:
#'s are ignored linesCode:#test.dat [other] percent=1



LinkBack URL
About LinkBacks


