Reading words from a text file

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

    Reading words from a text file

    I've came up with this:

    FILE *fd;
    char buf[100];
    char fmt[10];

    /* open fd */
    sprintf(fmt, "%%%ds", sizeof(buf)-1);
    while (fscanf(fd, fmt, buf) != EOF)
    use_word(strdup (buf));

    close(fd);

    Is it the best way? What do you think?
    Thanks,
    Mattia
  • Ben Bacarisse

    #2
    Re: Reading words from a text file

    mattia <gervaz@gmail.c omwrites:
    I've came up with this:
    >
    FILE *fd;
    char buf[100];
    char fmt[10];
    >
    /* open fd */
    sprintf(fmt, "%%%ds", sizeof(buf)-1);
    while (fscanf(fd, fmt, buf) != EOF)
    use_word(strdup (buf));
    >
    close(fd);
    >
    Is it the best way? What do you think?
    Define "best"?! It is one way (I'd test fscanf(...) == 1 rather != EOF
    but that is a small matter) but only you know if it is the best for
    your purpose.

    You can embed macro into a format string using the # operator and
    string literal concatenation ("%" "32" "s" is the same literal as
    "%32s"). But that requires an extra function-like macro and adding 1
    when the macro is used to define the array. I think the details are
    in the FAQ.

    Personally, I'd just write a 'int get_word(FILE *fp, char *buf, size_t
    size);' function. It is not hard, and you'll use it 1000 (and
    re-write it 10) times before you retire. It is odd, but C seems to
    encourage this sort of build your own private tool-kit approach.

    --
    Ben.

    Comment

    • Eric Sosman

      #3
      Re: Reading words from a text file

      mattia wrote:
      I've came up with this:
      >
      FILE *fd;
      char buf[100];
      char fmt[10];
      >
      /* open fd */
      sprintf(fmt, "%%%ds", sizeof(buf)-1);
      while (fscanf(fd, fmt, buf) != EOF)
      use_word(strdup (buf));
      >
      close(fd);
      >
      Is it the best way? What do you think?
      You shouldn't use "%d" with a size_t value and you
      should use fclose instead of close, but those are easily
      fixed. I'd worry about the (non-Standard) strdup function
      returning a NULL, and I'd probably write use_word(buf) and
      leave it up to use_word to decide whether to make a copy,
      but that's probably not what you're asking about.

      An alternative (I don't say it's better or worse, just
      that there are other ways):

      #define MAXWORD 99
      #define STRING(x) STRING_HELPER(x )
      #define STRING_HELPER(x ) #x
      ...
      char buf[MAXWORD+1];
      while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
      ...

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • mattia

        #4
        Re: Reading words from a text file

        On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
        mattia wrote:
        >I've came up with this:
        >>
        >FILE *fd;
        >char buf[100];
        >char fmt[10];
        >>
        >/* open fd */
        >sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
        >EOF)
        > use_word(strdup (buf));
        >>
        >close(fd);
        >>
        >Is it the best way? What do you think?
        >
        You shouldn't use "%d" with a size_t value and you
        should use fclose instead of close, but those are easily fixed. I'd
        worry about the (non-Standard) strdup function returning a NULL, and I'd
        probably write use_word(buf) and leave it up to use_word to decide
        whether to make a copy, but that's probably not what you're asking
        about.
        >
        An alternative (I don't say it's better or worse, just
        that there are other ways):
        >
        #define MAXWORD 99
        #define STRING(x) STRING_HELPER(x )
        #define STRING_HELPER(x ) #x
        ...
        char buf[MAXWORD+1];
        while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
        ...
        Great, I'll use this trick, thanks.

        --
        __mattia__

        Comment

        • stdazi@gmail.com

          #5
          Re: Reading words from a text file

          I just hope you free the reference to the string in use_word
          On Apr 9, 12:14 pm, mattia <ger...@gmail.c omwrote:
          I've came up with this:
          >
          FILE *fd;
          char buf[100];
          char fmt[10];
          >
          /* open fd */
          sprintf(fmt, "%%%ds", sizeof(buf)-1);
          while (fscanf(fd, fmt, buf) != EOF)
                  use_word(strdup (buf));
          >
          close(fd);
          >
          Is it the best way? What do you think?
          Thanks,
          Mattia

          Comment

          • Barry Schwarz

            #6
            Re: Reading words from a text file

            On 09 Apr 2008 13:21:21 GMT, mattia <gervaz@gmail.c omwrote:
            >On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
            >
            >mattia wrote:
            >>I've came up with this:
            >>>
            >>FILE *fd;
            >>char buf[100];
            >>char fmt[10];
            >>>
            >>/* open fd */
            >>sprintf(fmt , "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
            >>EOF)
            >> use_word(strdup (buf));
            >>>
            >>close(fd);
            >>>
            >>Is it the best way? What do you think?
            >>
            > You shouldn't use "%d" with a size_t value and you
            >should use fclose instead of close, but those are easily fixed. I'd
            >worry about the (non-Standard) strdup function returning a NULL, and I'd
            >probably write use_word(buf) and leave it up to use_word to decide
            >whether to make a copy, but that's probably not what you're asking
            >about.
            >>
            > An alternative (I don't say it's better or worse, just
            >that there are other ways):
            >>
            > #define MAXWORD 99
            > #define STRING(x) STRING_HELPER(x )
            > #define STRING_HELPER(x ) #x
            > ...
            > char buf[MAXWORD+1];
            > while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
            > ...
            >
            >Great, I'll use this trick, thanks.
            You do realize that any use of *scanf to read a string will stop at
            the first white space character. Since you mentioned a function
            called use_word, maybe you really do want to stop at the space that
            terminates the current word.

            However if you want to read a string that contains embedded blanks,
            you may find fgets more suitable.


            Remove del for email

            Comment

            • mattia

              #7
              Re: Reading words from a text file

              On Wed, 09 Apr 2008 08:20:44 -0700, Barry Schwarz wrote:
              On 09 Apr 2008 13:21:21 GMT, mattia <gervaz@gmail.c omwrote:
              >
              >>On Wed, 09 Apr 2008 08:29:07 -0400, Eric Sosman wrote:
              >>
              >>mattia wrote:
              >>>I've came up with this:
              >>>>
              >>>FILE *fd;
              >>>char buf[100];
              >>>char fmt[10];
              >>>>
              >>>/* open fd */
              >>>sprintf(fm t, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
              >>>EOF)
              >>> use_word(strdup (buf));
              >>>>
              >>>close(fd);
              >>>>
              >>>Is it the best way? What do you think?
              >>>
              >> You shouldn't use "%d" with a size_t value and you
              >>should use fclose instead of close, but those are easily fixed. I'd
              >>worry about the (non-Standard) strdup function returning a NULL, and
              >>I'd probably write use_word(buf) and leave it up to use_word to decide
              >>whether to make a copy, but that's probably not what you're asking
              >>about.
              >>>
              >> An alternative (I don't say it's better or worse, just
              >>that there are other ways):
              >>>
              >> #define MAXWORD 99
              >> #define STRING(x) STRING_HELPER(x )
              >> #define STRING_HELPER(x ) #x
              >> ...
              >> char buf[MAXWORD+1];
              >> while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
              >> ...
              >>
              >>Great, I'll use this trick, thanks.
              >
              You do realize that any use of *scanf to read a string will stop at the
              first white space character. Since you mentioned a function called
              use_word, maybe you really do want to stop at the space that terminates
              the current word.
              >
              However if you want to read a string that contains embedded blanks, you
              may find fgets more suitable.
              >
              >
              Remove del for email
              Actually I've got to read the words used in /usr/share/dict/words



              --
              __mattia__

              Comment

              • mattia

                #8
                Re: Reading words from a text file

                On Wed, 09 Apr 2008 08:10:43 -0700, stdazi@gmail.co m wrote:
                I just hope you free the reference to the string in use_word On Apr 9,
                12:14 pm, mattia <ger...@gmail.c omwrote:
                >I've came up with this:
                >>
                >FILE *fd;
                >char buf[100];
                >char fmt[10];
                >>
                >/* open fd */
                >sprintf(fmt, "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
                >EOF)
                >        use_word(strdup (buf));
                >>
                >close(fd);
                >>
                >Is it the best way? What do you think? Thanks,
                >Mattia
                Hmn, actually no, maybe it is better if I call use_word(buf) and then
                decide to copy the buffer using strdup(), then I'll free the pointer
                returned.



                --
                __mattia__

                Comment

                • CBFalconer

                  #9
                  Re: Reading words from a text file

                  Barry Schwarz wrote:
                  mattia <gervaz@gmail.c omwrote:
                  >Eric Sosman wrote:
                  >>mattia wrote:
                  >>>
                  >>>I've came up with this:
                  >>>>
                  >>>FILE *fd;
                  >>>char buf[100];
                  >>>char fmt[10];
                  >>>>
                  >>>/* open fd */
                  >>>sprintf(fm t, "%%%ds", sizeof(buf)-1);
                  >>>while (fscanf(fd, fmt, buf) != EOF)
                  >>> use_word(strdup (buf));
                  >>>>
                  >>>close(fd);
                  >>>>
                  >>>Is it the best way? What do you think?
                  >>>
                  >>You shouldn't use "%d" with a size_t value and you should use
                  >>fclose instead of close, but those are easily fixed. I'd worry
                  >>about the (non-Standard) strdup function returning a NULL, and
                  >>I'd probably write use_word(buf) and leave it up to use_word to
                  >>decide whether to make a copy, but that's probably not what
                  >>you're asking about.
                  >>>
                  >>An alternative (I don't say it's better or worse, just
                  >>that there are other ways):
                  >>>
                  >> #define MAXWORD 99
                  >> #define STRING(x) STRING_HELPER(x )
                  >> #define STRING_HELPER(x ) #x
                  >> ...
                  >> char buf[MAXWORD+1];
                  >> while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
                  >> ...
                  >>
                  >Great, I'll use this trick, thanks.
                  >
                  You do realize that any use of *scanf to read a string will stop
                  at the first white space character. Since you mentioned a
                  function called use_word, maybe you really do want to stop at
                  the space that terminates the current word.
                  >
                  However if you want to read a string that contains embedded
                  blanks, you may find fgets more suitable.
                  He might consider using ggets() instead, which returns an allocated
                  buffer that you can use until you 'free' it. Written in standard C,
                  and fully portable. See:

                  <http://cbfalconer.home .att.net/download/>

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.


                  ** Posted from http://www.teranews.com **

                  Comment

                  • mattia

                    #10
                    Re: Reading words from a text file

                    On Wed, 09 Apr 2008 21:07:40 -0400, CBFalconer wrote:
                    Barry Schwarz wrote:
                    >mattia <gervaz@gmail.c omwrote:
                    >>Eric Sosman wrote:
                    >>>mattia wrote:
                    >>>>
                    >>>>I've came up with this:
                    >>>>>
                    >>>>FILE *fd;
                    >>>>char buf[100];
                    >>>>char fmt[10];
                    >>>>>
                    >>>>/* open fd */
                    >>>>sprintf(fmt , "%%%ds", sizeof(buf)-1); while (fscanf(fd, fmt, buf) !=
                    >>>>EOF)
                    >>>> use_word(strdup (buf));
                    >>>>>
                    >>>>close(fd) ;
                    >>>>>
                    >>>>Is it the best way? What do you think?
                    >>>>
                    >>>You shouldn't use "%d" with a size_t value and you should use fclose
                    >>>instead of close, but those are easily fixed. I'd worry about the
                    >>>(non-Standard) strdup function returning a NULL, and I'd probably
                    >>>write use_word(buf) and leave it up to use_word to decide whether to
                    >>>make a copy, but that's probably not what you're asking about.
                    >>>>
                    >>>An alternative (I don't say it's better or worse, just that there are
                    >>>other ways):
                    >>>>
                    >>> #define MAXWORD 99
                    >>> #define STRING(x) STRING_HELPER(x )
                    >>> #define STRING_HELPER(x ) #x
                    >>> ...
                    >>> char buf[MAXWORD+1];
                    >>> while (fscanf(fd, "%" STRING(MAXWORD) "s", buf) != EOF)
                    >>> ...
                    >>>
                    >>Great, I'll use this trick, thanks.
                    >>
                    >You do realize that any use of *scanf to read a string will stop at the
                    >first white space character. Since you mentioned a function called
                    >use_word, maybe you really do want to stop at the space that terminates
                    >the current word.
                    >>
                    >However if you want to read a string that contains embedded blanks, you
                    >may find fgets more suitable.
                    >
                    He might consider using ggets() instead, which returns an allocated
                    buffer that you can use until you 'free' it. Written in standard C, and
                    fully portable. See:
                    >
                    <http://cbfalconer.home .att.net/download/>
                    Great, thanks.



                    --
                    Mattia

                    Comment

                    Working...