Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main(int argc, char **argv) {
  6.  
  7.     FILE * fPointer;
  8.     fPointer = fopen(argv[1], "r");  // read from file
  9.  
  10.     char singleLine[30];
  11.     char section[30];
  12.     char key[30];
  13.     int right_section = 0;
  14.     char current_section[30];
  15.     int key_value_found = 0;
  16.     int section_found = 0;
  17.  
  18.     sscanf(argv[2], "%[a-zA-Z0-9].%[a-zA-Z0-9]", section, key); //split of section.key
  19.  
  20.     while(!feof(fPointer)){
  21.         fgets(singleLine, 30, fPointer); //read line by line
  22.  
  23.         char current_key[30];
  24.         char current_value[30];
  25.         if(singleLine[0]=='[' && right_section==1){
  26.             right_section = 0;
  27.         }
  28.  
  29.         else if(singleLine[0]=='['){
  30.             sscanf(singleLine, "[%127[^]]", current_section); //strip from []
  31.             if(strcmp(section, current_section)==0){
  32.                 right_section = 1;
  33.                 section_found = 1;
  34.             }
  35.         }
  36.  
  37.         else if(right_section == 1 && singleLine[0]!=';' && key_value_found==0){
  38.             sscanf(singleLine, "%127s = %127s", current_key, current_value);
  39.             if(strcmp(current_key,key)==0){
  40.                 printf("The value you are looking for:%s%s", current_value, "\n");     
  41.                 key_value_found = 1;       
  42.             }
  43.         }
  44.     }
  45.  
  46.     if(section_found==0){
  47.         fprintf (stderr, "error: Failed to find %s\n", section);
  48.     }
  49.     if(section_found==1 && key_value_found==0){
  50.         fprintf (stderr, "error: Failed to find %s in %s\n",key, section);
  51.     }
  52.    
  53.     fclose(fPointer);
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement