Thread: My ini load code.

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14

    Lightbulb My ini load code.

    I wrote an ini file reader for my game im working on and got it working fine(Easier done than said for a change). I am wondering if there is a way to make my code read everything between " " as a single string instead of word by word. Currently every white space divides each word. My code may be ugly and not efficient but it gets what I want done.

    Code:
    void ini_open_read(char fileOpen[], char Temp[][200]){
        FILE *iniP;
        iniP = fopen(fileOpen,"r");
        if(iniP == NULL){
           fprintf(stderr,"Cant open file %s\n", fileOpen);
           system("pause");
           exit(1);
        }
        int i = 0;
        while(fscanf(iniP,"%s", Temp[i]) !=EOF){
           i++;
        }
        fclose(iniP);
    }
    
    void ini_find_value(char section[],char name[],char value[],char Temp[][200]){
        int n = 0;
        while(strcmp(Temp[n],section)){
           if(strcmp(Temp[n],"[End]")){
           n++;}
        }
        while(strcmp(Temp[n],name)){
           if(strcmp(Temp[n],"[End]")){
           n++;}
           
        }
        if (!strcmp(Temp[n],name)){
           n++;
           n++;
        }
        
        if (strcmp(Temp[n],"[End]")){
        strcpy(value,Temp[n]);}
        if (!strcmp(Temp[n],"[End]")){
        printf("Error come to unexpected end of file");}
     
    }
    Thanks
    RMDan

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look up "scan set" in the *scanf manual.
    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
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    And were may I find that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by RMDan View Post
    And were may I find that?
    "man scanf"?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The same place that you figured out what %s did.
    It's very closely related to %s, so it shouldn't be far away.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    I have no clue what you guys are talking about. I learned most of my stuff from youtube and google.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    ! Aware to man pages: scanf(3)
    Start collecting bookmarks of where you can find information on functions you use.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    I know you guys are trying to help but could you give me an example? What you gave me told me nothing I wanted(only stuff I mostly knew already). the [ one is what I may need but how would I use it how I want?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by RMDan View Post
    I know you guys are trying to help but could you give me an example? What you gave me told me nothing I wanted(only stuff I mostly knew already). the [ one is what I may need but how would I use it how I want?
    I'm pretty sure I've posted half a dozen examples already on this board. Could you find them?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    fscanf( file, "\"%[^\"]\"", string );
    The red bits are literal " characters
    The blue bit is a complemented scan set, which is basically matching everything EXCEPT a literal "

    So this would match
    "this is a nice long string with spaces in double quotes"
    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.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    so far if I replace %s with \"%[^\"]\" my load system freezes as not everything is in " ". Thanks for trying.
    Code:
    void main(void){
       char pie[20];
       gets(pie);
       switch(pie){
          case1: (!strcmp(pie,"Apple");
          printf("All is Good");
          break;
          case2: (!strcmp(pie,"Cherry");
          printf("Mmm");
          break;
          case3: (!strcmp(pie,"3.14");
          printf("Thats math not pie");
          break;
          default:
          printf("Give Me Pie");
       }
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main(void)
    > gets(pie);
    See the FAQ - both these are disasters.

    > so far if I replace %s with \"%[^\"]\" my load system freezes as not everything is in " "
    Which is why you should begin by reading a line using fgets(), then try to parse the line from memory.

    If you have any kind of variability in the input file format, then using fscanf() is wrong. As soon as you guess wrong as to what is coming next, then you have an awful job of undoing the last half-assed attempt that fscanf made with the input stream.

    With fgets(), you read a whole line into memory, and then you can have as many goes as you want at decoding it.
    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.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    First: my signature is an abstract Joke at pie and not part of this discusion.

    Second: So I have to rewrite my whole code to get it to work my way or live with what ive got. Thanks for the help. So fgets() reads in strings line by line. That may be an easier method than the one I am using now. In the rewrite I could just drop the " " even.

    RMDan
    Code:
    void main(void){
       char pie[20];
       gets(pie);
       switch(pie){
          case1: (!strcmp(pie,"Apple");
          printf("All is Good");
          break;
          case2: (!strcmp(pie,"Cherry");
          printf("Mmm");
          break;
          case3: (!strcmp(pie,"3.14");
          printf("Thats math not pie");
          break;
          default:
          printf("Give Me Pie");
       }
    }

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by RMDan View Post
    I have no clue what you guys are talking about. I learned most of my stuff from youtube and google.
    Dude. I typed "scanf scan set" into Google and the first hit answers your question. Jeez.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    IMO, your signature is too long then.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. How to write a C code to load an image file.
    By dv007 in forum C Programming
    Replies: 4
    Last Post: 05-25-2002, 05:27 PM
  5. How-To: Load Pictures in MFC. Code Inside.
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2002, 09:17 PM