Reading a file...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CBFalconer

    #16
    Re: Reading a file...

    EdUarDo wrote:[color=blue]
    >
    > Hello again, thanks to all for your answers. I've tried to follow
    > all of your advices and this is the result:
    >[/color]
    .... snip ...[color=blue]
    >
    > // Opening the file
    > if((fp = fopen("fichero_ test.txt", "rb")) != NULL) {
    > // Reseve memory[/color]
    .... snip long extended code ...[color=blue]
    >
    > } else {
    > return EXIT_FAILURE;
    > }[/color]

    Here's a readability tip. When you have a long clause, and a short
    clause, arrange to have the short clause first. That way the
    reader can quickly determine what controls entry to any particular
    clause. The above could be:

    if (NULL == (fp = fopen("fichero_ test.txt", "rb")))
    return EXIT_FAILURE;
    else {
    ... long extended code ...
    }

    --
    "The power of the Executive to cast a man into prison without
    formulating any charge known to the law, and particularly to
    deny him the judgement of his peers, is in the highest degree
    odious and is the foundation of all totalitarian government
    whether Nazi or Communist." -- W. Churchill, Nov 21, 1943


    Comment

    • Ben Bacarisse

      #17
      Re: Reading a file...

      On Thu, 23 Mar 2006 10:28:24 +0100, EdUarDo wrote:
      [color=blue]
      > // Read a number
      > while (read = fscanf(fp, "%lf", &number) != EOF) {
      > if (read == 1) {[/color]

      This looks a little odd although it is fine. There is no reason to assign
      to read the value of the test (fscanf(...) != EOF) since you use read only
      on the next line and you can't get to the next line unless read is 1.

      while (fscanf(fp, "%lf", &number) != EOF) {

      does the same job.

      --
      Ben.

      Comment

      • jaysome

        #18
        Re: Reading a file...

        CBFalconer wrote:
        [color=blue]
        > EdUarDo wrote:
        >[color=green]
        >>Hello again, thanks to all for your answers. I've tried to follow
        >>all of your advices and this is the result:
        >>[/color]
        >
        > ... snip ...
        >[color=green]
        >> // Opening the file
        >> if((fp = fopen("fichero_ test.txt", "rb")) != NULL) {
        >> // Reseve memory[/color]
        >
        > ... snip long extended code ...
        >[color=green]
        >> } else {
        >> return EXIT_FAILURE;
        >> }[/color]
        >
        >
        > Here's a readability tip. When you have a long clause, and a short
        > clause, arrange to have the short clause first. That way the
        > reader can quickly determine what controls entry to any particular
        > clause. The above could be:
        >
        > if (NULL == (fp = fopen("fichero_ test.txt", "rb")))
        > return EXIT_FAILURE;
        > else {
        > ... long extended code ...
        > }[/color]

        My preference is to avoid that style altogether:

        fp = fopen("fichero_ test.txt", "rb");
        if ( !fp )
        {
        return EXIT_FAILURE;
        }
        else
        {
        ... long extended code ...
        }

        or just:

        if ( !fp )
        {
        return EXIT_FAILURE;
        }

        .... long extended code ...

        This makes it easier for debugging and jives well with things like
        PC-lint and MISRA rules.

        YMMV.

        --
        jay

        Comment

        • Herbert Rosenau

          #19
          Re: Reading a file...

          On Wed, 22 Mar 2006 18:09:45 UTC, "Fred Kleinschmidt"
          <fred.l.kleinms chmidt@boeing.c om> wrote:
          [color=blue][color=green]
          > > // copy the file into the buffer.
          > > fread (buffer,1,lSize ,pFile);[/color]
          >
          > Why are yoiu using fread? fread is for reading binary data.
          > You have already stated that the file is text.[/color]

          No, fread is used to read the specified number of blocks in specified
          length. fopen() specifies if the file is to read as binary (no
          transation is made) or as test (some characters like cr, lf are
          translated to the system specific layout.

          Yeah, the OP uses the wrong I/O method to get data in as the data he
          reads is not of constant length and even the length of the date he has
          to read in a line is not constant.

          I would not try to read using fscanf() because the variant number of
          floats in a line, but would siply use fgetc() like

          #define MAXFLOATCHARS 15 /* a double may been ... chars */

          /* read a number of of doubles from a file, whereas */
          /* the file contains a unknown number of doubles, */
          /* separated with white spaces */
          /* return: pointer to array containing readed doubles */
          /* return parameters: */
          /* pointer to number of readed doubles */
          /* */
          /* remark: caller has to free() result */
          double *doubles read_doubles(un signed int *count_doubles) {
          int c;
          unsigned int in_double = 0; /* count no. of chars stored */
          unsigned int reserved doubles = 0;
          char double_as_text[MAXDOUBLECHARS+ 1]
          double *doubles = NULL;

          while ((c = fgets(file)) != EOF) {
          if (isspace(c)) { /* possible delemitters */
          if (in_double) {
          /* at least one digit or decimal delemitter readed */
          double_as_text[in_double] = '\0';
          doubles = conv_a_double(d ouble_as_text,
          doubles,
          &count_doubl es,
          &reserved_doubl es);
          }
          in_double = 0;
          continue;
          }
          /* missing: test of guilty char that is possible
          for a double (e.g. one decimal delemitter,
          exponent char,.... */
          double_as_text[in_double++] = c;
          }
          if (in_double) {
          double_as_text[in_double] = '\0';
          doubles = conv_a_double(d ouble_as_text, doubles, &count_doubl es,
          &reserved_doubl es);
          }
          return doubles;
          }

          /* convert a double from string */
          /* parameters: */
          /* char *double_as_text string holding a double to convert */
          /* double *doubles pointer to array holding doubles */
          /* unsigned int count_doubles pointer to no. of readed doubles */
          /* unsigned int reserved doubles pointer to no, of reserved d. */
          /* return: pointer to array of doubles stored */
          /* return parameter: number of doubles reserved in no_of_doubles */
          double* conv_a_double(c har *double_as_text ,
          double *doubles,
          unsigned int *count_doubles,
          unsigned int *reserved doubles) {
          double *result;
          if ((*reserved_dou bles == count_doubles) {
          result = realloc(*double s,
          (*reserved_doub les += 100) * sizeof(*result) )
          if (!result) {
          fprintf("failed to allocate enough memory\n");
          exit(EXIT_FAILT URE);
          }
          *doubles = result;
          }
          sscanf(double_a s_text, "%f", &doubles[*count_doubles+ +]);
          return doubles;
          }

          Untested!

          --
          Tschau/Bye
          Herbert

          Visit http://www.ecomstation.de the home of german eComStation
          eComStation 1.2 Deutsch ist da!

          --
          Tschau/Bye
          Herbert

          Visit http://www.ecomstation.de the home of german eComStation
          eComStation 1.2 Deutsch ist da!

          Comment

          • Richard Bos

            #20
            Re: Reading a file...

            CBFalconer <cbfalconer@yah oo.com> wrote:
            [color=blue]
            > EdUarDo wrote:[color=green]
            > >
            > > // Opening the file
            > > if((fp = fopen("fichero_ test.txt", "rb")) != NULL) {
            > > // Reseve memory[/color][/color]
            [color=blue]
            > Here's a readability tip. When you have a long clause, and a short
            > clause, arrange to have the short clause first. That way the
            > reader can quickly determine what controls entry to any particular
            > clause. The above could be:
            >
            > if (NULL == (fp = fopen("fichero_ test.txt", "rb")))[/color]

            Ew. Shades of RPN.

            Richard

            Comment

            • Micah Cowan

              #21
              Re: Reading a file...

              Ben Bacarisse <ben.usenet@bsb .me.uk> writes:
              [color=blue]
              > On Thu, 23 Mar 2006 10:28:24 +0100, EdUarDo wrote:
              >[color=green]
              > > // Read a number
              > > while (read = fscanf(fp, "%lf", &number) != EOF) {
              > > if (read == 1) {[/color]
              >
              > This looks a little odd although it is fine. There is no reason to assign
              > to read the value of the test (fscanf(...) != EOF) since you use read only
              > on the next line and you can't get to the next line unless read is 1.
              >
              > while (fscanf(fp, "%lf", &number) != EOF) {
              >
              > does the same job.[/color]

              It seems very likely that EdUarDo wanted:

              while((read = fscanf(fp, "%lf", &number)) != EOF) {

              In which case, he'll still want to test read on the next line.

              Comment

              • Ben Bacarisse

                #22
                Re: Reading a file...

                On Fri, 24 Mar 2006 20:19:15 +0000, Micah Cowan wrote:
                [color=blue]
                > Ben Bacarisse <ben.usenet@bsb .me.uk> writes:
                >[color=green]
                >> On Thu, 23 Mar 2006 10:28:24 +0100, EdUarDo wrote:
                >>[color=darkred]
                >> > // Read a number
                >> > while (read = fscanf(fp, "%lf", &number) != EOF) {
                >> > if (read == 1) {[/color]
                >>
                >> This looks a little odd although it is fine. There is no reason to assign
                >> to read the value of the test (fscanf(...) != EOF) since you use read only
                >> on the next line and you can't get to the next line unless read is 1.
                >>
                >> while (fscanf(fp, "%lf", &number) != EOF) {
                >>
                >> does the same job.[/color]
                >
                > It seems very likely that EdUarDo wanted:
                >
                > while((read = fscanf(fp, "%lf", &number)) != EOF) {
                >
                > In which case, he'll still want to test read on the next line.[/color]

                I agree, and I thought that was the original intent also, but since the
                OP's code took no action when 0 is returned (the only other possible value
                in this case) I concluded that they were content to assume the input
                contains no "matching errors" and I consequently simplified what I had to
                write by only offering an equivalent piece of code.

                A better answer might have been to add the parentheses and suggest an
                action to take when read == 0, or to suggest

                while (fscanf(fp, "%lf", &number) == 1)

                which at least terminates on all possible inputs! Yes, I should at least
                have suggested that.

                --
                Ben.

                Comment

                Working...