how to convert char* to File *

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cnwy@263.net

    how to convert char* to File *

    Hi,
    I have a long string in char * and want to convert it to a File *
    fp.
    Is there any best way to do it without using tmpfile?

    Thanks.

    wy.

  • michaelquinlivan@gmail.com

    #2
    Re: how to convert char* to File *


    cnwy@263.net wrote:
    Hi,
    I have a long string in char * and want to convert it to a File *
    fp.
    Is there any best way to do it without using tmpfile?
    >
    Thanks.
    >
    wy.
    Does this question make sense to anyone else? Are you trying to open a
    file?

    Comment

    • Nils O. Selåsdal

      #3
      Re: how to convert char* to File *

      cnwy@263.net wrote:
      Hi,
      I have a long string in char * and want to convert it to a File *
      fp.
      Is there any best way to do it without using tmpfile?
      >
      Thanks.
      There is no standard way to slap a FILE* interface on top of an array.
      (some systems provide an extension for that).
      You can write the content of your array to a file and open that
      though - as you hint you have discovered with your references to
      'tmpfile'.

      Then again it isn't that clear what you really need.

      Comment

      • Keith Thompson

        #4
        Re: how to convert char* to File *

        cnwy@263.net writes:
        Hi,
        I have a long string in char * and want to convert it to a File *
        fp.
        Is there any best way to do it without using tmpfile?
        I presume you mean FILE*, not File*.

        What do you mean by "convert"? You can convert a char* to a FILE*
        with a cast operator, but the result isn't going to be meaningful.
        You can write a string to a file, but that's not what I'd call a
        conversion. What exactly are you trying to accomplish?

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • phus

          #5
          Re: how to convert char* to File *


          cnwy@263.net wrote:
          Hi,
          I have a long string in char * and want to convert it to a File *
          fp.
          Is there any best way to do it without using tmpfile?
          >
          Thanks.
          >
          wy.
          perhaps fmemopen(3) is you want.
          please see
          http://www.gnu.org/software/libc/man...g-Streams.html.

          Comment

          • Richard Riley

            #6
            Re: how to convert char* to File *

            cnwy@263.net writes:
            Hi,
            I have a long string in char * and want to convert it to a File *
            fp.
            Is there any best way to do it without using tmpfile?
            >
            Thanks.
            >
            wy.
            >
            I suspect you really want to know how to save and restore text to a
            file? Your question doesnt really make sense so I have made a guess at
            what you want based on it-


            A computer program is the key to the digital city: If you know the language, you can get a computer to do almost anything you want. Learn how to write computer programs in C.


            might help!

            If it doesnt, then maybe you could explain why you need a File * and
            what you intend it to be pointing at?

            They assume you know some C : there are many books and tutorials
            available. Use google.

            Good luck!

            Comment

            • santosh

              #7
              Re: how to convert char* to File *

              cnwy@263.net wrote:
              Hi,
              I have a long string in char * and want to convert it to a File * fp.
              Is there any best way to do it without using tmpfile?
              No, there's no portable way to do it without opening a file and writing
              the string to it. OTOH, if you're not concerned about portability you
              might consider the GNU C extension "phus" has mentioned in his reply to
              you.

              Comment

              • cnwy@263.net

                #8
                Re: how to convert char* to File *


                Sorry,I mean FILE *fp actrually.
                There is a c function of a legacy system,which only accept FILE *fp
                parameter and parse the content,and now the new system also provide
                char* for the content,and I think saving char* to a file and opening it
                to get FILE *fp then call the function is poor performance. so I doubt
                is there any way to directly push char* into the stream without disk
                reading and writing.

                Comment

                • santosh

                  #9
                  Re: how to convert char* to File *

                  c...@263.net wrote:
                  Please quote the post to which you're replying since not all
                  participants of Usenet can as easily access previous posts as users of
                  Google Groups.
                  Sorry,I mean FILE *fp actrually.
                  There is a c function of a legacy system,which only accept FILE *fp
                  parameter and parse the content,
                  As far as I know, the FILE interface was standardised by ANSI. So your
                  "legacy" system must be an ancient UNIX implementation.
                  and now the new system also provide
                  char* for the content,
                  A char * is _very_ different from a FILE *. The latter is an
                  implementation specific abstract data type which is by necessity
                  associated with streams and is the only method to do I/O in standard C.
                  The former is simply a pointer to type char.
                  and I think saving char* to a file and opening it
                  to get FILE *fp then call the function is poor performance.
                  Why do you think so? Have you actually done measurements and concluded
                  that the speed requirements of your program are higher than what
                  standard C can offer. This is very rarely the case.
                  so I doubt
                  is there any way to directly push char* into the stream without disk
                  reading and writing.
                  Yes, if you want to convert data pointed to by a char *, to the FILE
                  interface, the only standard way is to open a FILE stream and to copy
                  data from your char array into it and fclose() or fflush() it. This
                  should be fast enough for most purposes.

                  Comment

                  • Morris Dovey

                    #10
                    Re: how to convert char* to File *

                    cnwy@263.net (in
                    1151936662.8167 20.224200@h44g2 00...legr oups.com) said:

                    | Sorry,I mean FILE *fp actrually.
                    | There is a c function of a legacy system,which only accept FILE *fp
                    | parameter and parse the content,and now the new system also provide
                    | char* for the content,and I think saving char* to a file and
                    | opening it to get FILE *fp then call the function is poor
                    | performance. so I doubt is there any way to directly push char*
                    | into the stream without disk reading and writing.

                    If I'm understanding you correctly, you have something like:

                    int legacy_parse(FI LE *fp)
                    { char buffer[FILE_BUFFER_SIZ E];

                    /* Get data from file into buffer */

                    /* Parse data in buffer */

                    return result;
                    }

                    ....and you'd like to skip the step of writing the data to the file and
                    reading it back. It would seem that you could do that fairly easily by
                    simply passing the data that would have been read from the file to a
                    new version of the function that simply skips the file access, more or
                    less as follows:

                    int new_parse(char *buffer)
                    {
                    /* Parse data in buffer */

                    return result;
                    }

                    Where the /* Parse data in buffer */ logic remains unchanged. This is
                    probably (but not necessarily) an oversimplificat ion - but it's up to
                    you to keep track of the details. :-)

                    --
                    Morris Dovey
                    DeSoto Solar
                    DeSoto, Iowa USA



                    Comment

                    • santosh

                      #11
                      Re: how to convert char* to File *

                      Morris Dovey wrote:
                      cnwy@263.net (in
                      1151936662.8167 20.224200@h44g2 00...legr oups.com) said:
                      >
                      | Sorry,I mean FILE *fp actrually.
                      | There is a c function of a legacy system,which only accept FILE *fp
                      | parameter and parse the content,and now the new system also provide
                      | char* for the content,and I think saving char* to a file and
                      | opening it to get FILE *fp then call the function is poor
                      | performance. so I doubt is there any way to directly push char*
                      | into the stream without disk reading and writing.
                      >
                      If I'm understanding you correctly, you have something like:
                      >
                      int legacy_parse(FI LE *fp)
                      { char buffer[FILE_BUFFER_SIZ E];
                      >
                      /* Get data from file into buffer */
                      >
                      /* Parse data in buffer */
                      >
                      return result;
                      }
                      >
                      ...and you'd like to skip the step of writing the data to the file and
                      reading it back. It would seem that you could do that fairly easily by
                      simply passing the data that would have been read from the file to a
                      new version of the function that simply skips the file access, more or
                      less as follows:
                      Yes, the OP can do that unless the offending function is only available
                      in binary form.
                      The OP hasn't mentioned that yet.

                      Comment

                      • Morris Dovey

                        #12
                        Re: how to convert char* to File *

                        santosh (in 1151938480.5865 39.207680@b68g2 00...legr oups.com)
                        said:

                        | Morris Dovey wrote:
                        || cnwy@263.net (in
                        || 1151936662.8167 20.224200@h44g2 00...legr oups.com) said:
                        ||
                        ||| Sorry,I mean FILE *fp actrually.
                        ||| There is a c function of a legacy system,which only accept FILE
                        ||| *fp parameter and parse the content,and now the new system also
                        ||| provide char* for the content,and I think saving char* to a file
                        ||| and opening it to get FILE *fp then call the function is poor
                        ||| performance. so I doubt is there any way to directly push char*
                        ||| into the stream without disk reading and writing.
                        ||
                        || If I'm understanding you correctly, you have something like:
                        ||
                        || int legacy_parse(FI LE *fp)
                        || { char buffer[FILE_BUFFER_SIZ E];
                        ||
                        || /* Get data from file into buffer */
                        ||
                        || /* Parse data in buffer */
                        ||
                        || return result;
                        || }
                        ||
                        || ...and you'd like to skip the step of writing the data to the file
                        || and reading it back. It would seem that you could do that fairly
                        || easily by simply passing the data that would have been read from
                        || the file to a new version of the function that simply skips the
                        || file access, more or less as follows:
                        |
                        | Yes, the OP can do that unless the offending function is only
                        | available in binary form.
                        | The OP hasn't mentioned that yet.

                        Well, the OP did say that it was a _C_ function, and that its
                        parameters are known (and there hasn't yet been a request to
                        de-compile a binary. I'm being optimistic. :-)

                        --
                        Morris Dovey
                        DeSoto Solar
                        DeSoto, Iowa USA



                        Comment

                        • Chris Dollin

                          #13
                          Re: how to convert char* to File *

                          Morris Dovey wrote:
                          cnwy@263.net (in
                          1151936662.8167 20.224200@h44g2 00...legr oups.com) said:
                          >
                          | Sorry,I mean FILE *fp actrually.
                          | There is a c function of a legacy system,which only accept FILE *fp
                          | parameter and parse the content,and now the new system also provide
                          | char* for the content,and I think saving char* to a file and
                          | opening it to get FILE *fp then call the function is poor
                          | performance. so I doubt is there any way to directly push char*
                          | into the stream without disk reading and writing.
                          >
                          If I'm understanding you correctly, you have something like:
                          >
                          int legacy_parse(FI LE *fp)
                          { char buffer[FILE_BUFFER_SIZ E];
                          >
                          /* Get data from file into buffer */
                          >
                          /* Parse data in buffer */
                          >
                          return result;
                          }
                          >
                          ...and you'd like to skip the step of writing the data to the file and
                          reading it back.
                          You don't know that the legacy parser reads all the data into a
                          buffer and then parses it. It might extract the data from the
                          input one token at a time, and never materialise the entire file
                          (indeed, this might be because files might be bigger than the available
                          memory).

                          I see three possibilities.

                          (a) The OP can change the legacy code. In which case, what I'd do
                          is replace /all/ the stuff taking FILE* with stuff taking
                          Stream [Stream* for those who prefer], where Stream is a
                          type with useful operations like read/write/close/etc and
                          which can be implemented with different tactics such as
                          read from a FILE* or read from a buffer or read from a
                          compressed Stream or read from a concatenation of Streams
                          or ... Then the existing uses become uses of a Stream over
                          a FILE* and the new use is a Stream over a buffer.

                          If something can't work this way, fall back to (b...).

                          (b) The OP can't change the legacy code, but will always be
                          running on a system which provides an implementation-specific
                          "fake FILE* reading from a buffer"; in which case, use
                          that.

                          (c) The OP can't change the legacy code and can't use a
                          implementation-specific hack, but write/read a file is
                          fast enough [MEASURE]: use write/read.

                          (d) None of the above: renegotiate.

                          --
                          Chris "I remember OSPub. I /used/ OSPub." Dollin
                          "No-one here is exactly what he appears." G'kar, /Babylon 5/

                          Comment

                          • cnwy@263.net

                            #14
                            Re: how to convert char* to File *


                            santosh 写道:
                            c...@263.net wrote:
                            Please quote the post to which you're replying since not all
                            participants of Usenet can as easily access previous posts as users of
                            Google Groups.
                            >
                            Sorry,I mean FILE *fp actrually.
                            There is a c function of a legacy system,which only accept FILE *fp
                            parameter and parse the content,
                            >
                            As far as I know, the FILE interface was standardised by ANSI. So your
                            "legacy" system must be an ancient UNIX implementation.
                            >
                            and now the new system also provide
                            char* for the content,
                            >
                            A char * is _very_ different from a FILE *. The latter is an
                            implementation specific abstract data type which is by necessity
                            associated with streams and is the only method to do I/O in standard C.
                            The former is simply a pointer to type char.
                            >
                            and I think saving char* to a file and opening it
                            to get FILE *fp then call the function is poor performance.
                            >
                            Why do you think so? Have you actually done measurements and concluded
                            that the speed requirements of your program are higher than what
                            standard C can offer. This is very rarely the case.
                            managment of the tempfile is another reason,
                            the c function will be linked into .so and be called by java using jni
                            in multi-thread envirnoment.the concurrent transaction will create a
                            lot of tmp file and may be the bottle for the whole system.
                            >
                            so I doubt
                            is there any way to directly push char* into the stream without disk
                            reading and writing.
                            >
                            Yes, if you want to convert data pointed to by a char *, to the FILE
                            interface, the only standard way is to open a FILE stream and to copy
                            data from your char array into it and fclose() or fflush() it. This
                            should be fast enough for most purposes.
                            any code snippets for it?of course I don't want to open a real file to
                            do it.

                            Thank you for your help.

                            Comment

                            • cnwy@263.net

                              #15
                              Re: how to convert char* to File *


                              Morris Dovey 写道:
                              cnwy@263.net (in
                              1151936662.8167 20.224200@h44g2 00...legr oups.com) said:
                              >
                              | Sorry,I mean FILE *fp actrually.
                              | There is a c function of a legacy system,which only accept FILE *fp
                              | parameter and parse the content,and now the new system also provide
                              | char* for the content,and I think saving char* to a file and
                              | opening it to get FILE *fp then call the function is poor
                              | performance. so I doubt is there any way to directly push char*
                              | into the stream without disk reading and writing.
                              >
                              If I'm understanding you correctly, you have something like:
                              >
                              int legacy_parse(FI LE *fp)
                              { char buffer[FILE_BUFFER_SIZ E];
                              >
                              /* Get data from file into buffer */
                              >
                              /* Parse data in buffer */
                              >
                              return result;
                              }
                              it's something like:
                              FILE *fp;

                              fp = fopen(file, mode);
                              ....
                              and a lot of fscanf to do parsing.

                              >
                              ...and you'd like to skip the step of writing the data to the file and
                              reading it back. It would seem that you could do that fairly easily by
                              simply passing the data that would have been read from the file to a
                              new version of the function that simply skips the file access, more or
                              less as follows:
                              >
                              int new_parse(char *buffer)
                              {
                              /* Parse data in buffer */
                              >
                              return result;
                              }
                              >
                              Where the /* Parse data in buffer */ logic remains unchanged. This is
                              probably (but not necessarily) an oversimplificat ion - but it's up to
                              you to keep track of the details. :-)
                              >
                              --
                              Morris Dovey
                              DeSoto Solar
                              DeSoto, Iowa USA
                              http://www.iedu.com/DeSoto

                              Comment

                              Working...