Thread: Reading specific values in text file - C

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    13

    Reading specific values in text file - C

    Greetings everyone! So I'm programming in C for about 4~5 months now and yes, I'm pretty noob, and I'm having issues trying to read specific values inside of a .txt.
    Let me show you some lines of the text file to explain it better:


    Code:
    ...
    {015;scanner;7891150019508;;;29}
    {016;item;SHAMPOO CLEAR ANTICASPA MEN 200ML LIMP/PROFUN;UN;16,98;65}
    {017;scanner;7898016110041;;;29}  
    {018;item;LEITE UHT TRADICAO INTEGRAL 1 LT .;UN;3,38;53}
    ...
    (for some reason I needed to post it as 'code', first post on forum, sorry)

    The data that I need from the first two lines (015 and 016) are "7891150019508", "SHAMPOO CLEAR ANTICASPA MEN 200ML LIMP", "UN", "16,98", which means I need the code, description, unity and price.

    So I need to find these specific values that are between semicolon, and note that the first word on each line tells me what I need, if it's "scanner" then it will give me the code, and if it's "item" it will give me description, unity and price (always in that order), the last number on each line doesn't matter much.
    I believe I need to use fgets to read each line at the time and also strtok and strstr to make it work but I'm not very sure and I didn't understand how to actually use strtok. Can anyone give me some hints on how to procede?
    And by the way I'm brazilian and my english isn't very good, I hope you can understand all that I said .

  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
    Here is how you might use strtok.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
      char line[] = "{015;scanner;7891150019508;;;29}\n";
      const char *tok = ";\n";
      char *p;
      for ( p = strtok(line,tok) ; p != NULL ; p = strtok(NULL,tok) ) {
        printf("Token=%s\n",p);
      }
      // demonstrate that strtok changes line
      printf("Original line=%s\n",line);
      return 0;
    }
    
    
    
    $ gcc -Wall foo.c
    $ ./a.out 
    Token={015
    Token=scanner
    Token=7891150019508
    Token=29}
    Original line={015
    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
    Aug 2016
    Posts
    13

    Thumbs up

    Greetings @Salem!
    I managed to solve my problem without strtok (but I had the help of my uncle, and he is a very experienced programmer ), but I'm going to adapt my code with it because it will make so much easier when finding an 'item;' (since I will have three values in this line) if I use strtok, just need to put a int count inside the loop and store the third to fifth value, and now I understand and know how to use it!
    Here is my solution without the strtok:

    Code:
    char *result2;
    char *result3;
    
    
    void *LocalizaItem(char *str) //str = "{015;scanner;7891150019508;;;29}";
    {
        char * pini;
        char * pout;
        char * pt1;
        int pt2;
        int pt3;
        char buffer[1000], buffer2[1000], buffer3[1000];
        char *result;
        char nada[2] = "\0";
        GtkTextIter diter;
        
        /*======= Limpando globais no inicio da funcao =======*/
        
        result2 = (char *)malloc(strlen(nada)); 
        
            strcpy(result2,nada) ;
        //printf("result monitorado==>%s<==\n", result2);
        
        result3 = (char *)malloc(strlen(nada)); 
        
            strcpy(result3,nada) ;
        //printf("result monitorado==>%s<==\n", result3);
        /*====================================================*/
        
        pt1 = str;
       
        
      
        
        
        if(strstr(str,"scanner;") != NULL){
                printf("\n=======================================\n");
                printf("===== 'scanner' found in log =====\n");
                printf("=======================================\n");
            
            if((pini = strstr (str,"scanner;"))== NULL)
            {
                strcpy(buffer,"   ");            
                
            }
            
            
            pini = pini + (strlen("scanner;"));        
            
            pt2 = pini - pt1 ;        
            
                
                if((pout = strstr (pini,";"))== NULL)
                {
                    strcpy(buffer,"   ");                
                    
                }
                pt3 = pout - pt1 ;
            
            
                memset(buffer, 0, sizeof(buffer));                       
                           
                strncpy(buffer,pini,pt3-pt2);         
            
            }
    
        result = (char *)malloc(strlen(buffer) + 1); /* +1 = DFTTZ! */
        strcpy(result,buffer) ;
        printf("result==>%s<==\n", result);
    
    return result; //returning this => 7891150019508
    
    }
    The char *result2 and result3 are the variables I was using to store data if I recieve the "item;" in the line, and thats just a small part of the code, I actually created an if statement in every kinda of line I will recieve ("scanner;", "item;", "key;" and a few others).

    Thank you very much for your attention and time Salem!
    Last edited by kdan; 09-02-2016 at 02:48 PM.

  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
    So what is the purpose of result2, result3 and nada?

    Except to be a lot of small memory leaks.
    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
    Registered User
    Join Date
    Aug 2016
    Posts
    13
    Yeah, I put them (result2, result3) inside the function and removed nada (nada in portuguese means nothing), and its working as the same. And the purpose is to set their values into gtk entry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from specific word in text file
    By Nurlana in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2012, 09:24 PM
  2. Reading values from text file
    By a.mlw.walker in forum C Programming
    Replies: 5
    Last Post: 01-14-2012, 06:02 PM
  3. Reading a specific line from a text file
    By acidrain in forum C Programming
    Replies: 3
    Last Post: 12-01-2009, 02:23 PM
  4. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  5. Replies: 6
    Last Post: 11-11-2006, 02:10 PM

Tags for this Thread