Thread: Read a txt and find data

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Read a txt and find data

    Hi to all, I have a file like this one :

    ...
    ...
    ncfft=22492
    cr=-9.301750e+000
    fl=131072
    cpu=6976.765625
    prog=0.64155436
    potfreq=-1
    potactivity=0
    outfilepos=2113
    bs_power=188.316513
    bs_score=0.672828
    bs_bin=91475
    ...
    ...

    Now, i can read it with fgets untill i read the line i was looking for :

    prog=0.64155436

    But..how can I say "if I find a line with prog=, save it in an array" ?
    And once i've saved it, how can I modify that array for print, at the end, something like 64,15 (instead 0.64155436)

    I should do something like :

    Code:
    #include <stdio.h>
    
    int main() {
      char c[100];  /* declare a char array */
      FILE *file;  /* declare a FILE pointer  */
    
      file = fopen("file.txt", "r"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file);
        return 1;
      }
      else {
        printf("File opened successfully:\n\n");
        
        while(fgets(c, 100, file)!=NULL) { 
        
            //PSEUDOCODE
            IF c ="prog=*"
            copy c-->array
            modify array 
    
           }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        system ("PAUSE");
        return 0;
      }
    }
    thanx to everyone
    Last edited by BianConiglio; 04-24-2004 at 04:09 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) read into a line into a buffer.
    2) use something like strstr to check for "prog=".
    3) If it's there, do whatever with the buffer.
    4) Lather, rinse, repeat.

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

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I tried but i'm allready in trouble

    have a look :

    Code:
    #include <stdio.h>
    
    int main() {
      char c[100];  /* declare a char array */
      FILE *file;  /* declare a FILE pointer  */
    
      file = fopen("state.sah", "r"); 
      /* open a text file for reading */
    
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file); */
        return 1;
      }
      else {
        printf("File opened successfully:\n\n");
        
        while(fgets(c, 100, file)!=NULL) { 
        
            if(strstr("prog=0.",c)) printf("%c\n",c);
            
           }
    
        printf("\n\nNow closing file...\n\n");
        fclose(file);
        system ("PAUSE");
        return 0;
      }
    }
    in the file state.sah there is the line prog=0.64155436 but it is not found by my program (it is not found neighter if i write strstr("prog=0.64155436 ",c) ) where is the error ?

    thanx

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    "prog=0.64155436\n"
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    what a stupid i am !!! it is

    if(strstr(c,"prog=0.")) printf("%s",c);

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got your arguments backwards. Read the man page for a description of how strstr works.
    Code:
    if( strstr( wheretofindit, whattofind ) )
        printf("%c\n", c ); /* %c will only print one character */
    If you want the whole string, you use %s. Check the printf man page for format specifiers.

    [edit]I see you found your answer before I could press the button. All the colors slow me down. .[/edit]

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

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    really thanx to everyone

    now I have the problem to modify that string.....
    can someone tell me a nice site where to look for string operations ?

    I have to modify this "prog=0.64155436" in this 64,15%
    Last edited by BianConiglio; 04-24-2004 at 04:49 PM.

  8. #8
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Look (or search google) for string.h.

    I just googled myself anyways and came up with this page: string.h reference

    Goodluck.

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I have to modify this "prog=0.64155436" in this 64,15%
    look up strtok()

    get the second token delimited by '=' and just multiply by 100 (after of course converting to a float using something like atof), then sprintf back into a string like so:

    sprintf(buf,"prog=%f%%\n",new_float);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-17-2008, 11:38 AM
  2. Simple read from TXT
    By Gordon in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2008, 01:19 PM
  3. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  4. C++ and txt files
    By 182 in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2005, 03:15 PM
  5. writing/reading txt file
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2004, 09:34 PM