how to access record number and value using file handling in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • need some help
    New Member
    • Apr 2007
    • 12

    how to access record number and value using file handling in C?

    Hi ,

    Can any body help me here? I have 20 records in file and confidence values for each record like ,
    0.9,
    0.2,
    0.3,
    0.4
    ...

    and have to use those values in the other program as input. previously i had declaried array and put 20 values in that and accessing like confid[p], but I need my program to read each value from the file for eg (.confidence file) with Itemnumbers (1 to 20 ) so can you please tell me the code for that?

    I will really appreciate your help!

    Thanks.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by need some help
    Hi ,

    Can any body help me here? I have 20 records in file and confidence values for each record like ,
    0.9,
    0.2,
    0.3,
    0.4
    ...

    and have to use those values in the other program as input. previously i had declaried array and put 20 values in that and accessing like confid[p], but I need my program to read each value from the file for eg (.confidence file) with Itemnumbers (1 to 20 ) so can you please tell me the code for that?

    I will really appreciate your help!

    Thanks.
    There is no code telling here on this site,first make some efforts and show us what u tryed/did and then we will be more than happy to help u!

    If u don't know how to start check out text bellow
    The basic is in reading line by line until reaching EOF(End-Of-File) and populate a array with that data.Once populated u can access item number by array index.

    It would bee great if u also read posting guidelines for future referance on site "rules".

    Savage

    Comment

    • need some help
      New Member
      • Apr 2007
      • 12

      #3
      Thanks for reply.

      Originally posted by Savage
      There is no code telling here on this site,first make some efforts and show us what u tryed/did and then we will be more than happy to help u!

      If u don't know how to start check out text bellow
      The basic is in reading line by line until reaching EOF(End-Of-File) and populate a array with that data.Once populated u can access item number by array index.

      It would bee great if u also read posting guidelines for future referance on site "rules".

      Savage

      Comment

      • need some help
        New Member
        • Apr 2007
        • 12

        #4
        #include "defns.i"
        #include "types.i"
        #include "extern.i"

        char Delimiter;


        GetConfidence()
        {

        FILE *Nf, *fopen();
        char Fn[100], Buffer[1000];
        int confid[100];
        int item_num=0;

        srcpy(Fn,FileNa me);
        strcat(Fn,".con fidences")

        if( ! ( Nf = Fopen(Fn, "r") ) ) Error(0,Fn,"");

        /*Get confidences */

        do
        {

        ForEach(p,Fp,Lp )
        {
        confidence[p] = ????
        }


        }while (Delimiter == ',');

        This is my code and I would like to know how do I get the confidence that is declare in another file sample.confiden ce as :
        0.6,0.7,0.8.... ...

        which function here I should at the place of '????' to get these valuse from the file and at the end close the file.


        Thanks.

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by need some help
          #include "defns.i"
          #include "types.i"
          #include "extern.i"

          char Delimiter;


          GetConfidence()
          {

          FILE *Nf, *fopen();
          char Fn[100], Buffer[1000];
          int confid[100];
          int item_num=0;

          srcpy(Fn,FileNa me);
          strcat(Fn,".con fidences")

          if( ! ( Nf = Fopen(Fn, "r") ) ) Error(0,Fn,"");

          /*Get confidences */

          do
          {

          ForEach(p,Fp,Lp )
          {
          confidence[p] = ????
          }


          }while (Delimiter == ',');

          This is my code and I would like to know how do I get the confidence that is declare in another file sample.confiden ce as :
          0.6,0.7,0.8.... ...

          which function here I should at the place of '????' to get these valuse from the file and at the end close the file.


          Thanks.
          There are couple of methods.U can read char by char with fgetc char or use fgets to read in a whole line.If u are reading in char by char u should read only those chars which are not ,'\n'\t'.If u are reading in whole string u need to tokenize it using strtok()

          Savage

          Comment

          • need some help
            New Member
            • Apr 2007
            • 12

            #6
            I have just used fread() and it works. 'fgetc' I was not able use becasue it reads as a string. but here my values are float.

            Thanks.



            Originally posted by Savage
            There are couple of methods.U can read char by char with fgetc char or use fgets to read in a whole line.If u are reading in char by char u should read only those chars which are not ,'\n'\t'.If u are reading in whole string u need to tokenize it using strtok()

            Savage

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by need some help
              I have just used fread() and it works. 'fgetc' I was not able use becasue it reads as a string. but here my values are float.

              Thanks.
              There is the function atof which converts string to float,I forgot to mention that.

              Anyway,it works and that's all that matters right know.

              :)

              Savage

              Comment

              Working...