Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert function to read from string instead of file in C

Convert function to read from string instead of file in C

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelpquestionannouncement
4 Posts 4 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sahil Ranka
    wrote on last edited by
    #1

    I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure:

    static int LoadFromFile(FILE *Stream, ConfigStructure *cs)
    {
    int tempInt;

    ...

    if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 )
    {
    printf("Unable to read version number\n");
    return 0;
    }
    cs->Version = tempInt;
    ...

    }

    to one which allows us to bypass writing the configuration to disk and instead pass it directly in memory, roughly equivalent to this:

    static int LoadFromString(char *Stream, ConfigStructure *cs)

    A couple of things to note: • The current LoadFromFile function is incredibly dense and complex, reading dozens of versions of the config file in a backward-compatible manner, which makes duplication of the overall logic quite a pain. • The functions that generate the config file and those that read it originate in totally different parts of the old system and therefore don't share any data structures so I can't pass those directly. I could potentially write a wrapper, but again, it would need to handle any structure passed in a backward-compatible manner. • I'm tempted to just pass the file as is in as a string (as in the prototype above) and convert all the fscanf's to sscanf's but then I have to handle incrementing the pointer along (and potentially dealing with buffer overrun errors) manually. • This has to remain in C, so no C++ functionality like streams can help here Am I missing a superior choice? Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Any pointers, ideas or other assistance is enormously valuable.

    Mircea NeacsuM L D 3 Replies Last reply
    0
    • S sahil Ranka

      I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure:

      static int LoadFromFile(FILE *Stream, ConfigStructure *cs)
      {
      int tempInt;

      ...

      if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 )
      {
      printf("Unable to read version number\n");
      return 0;
      }
      cs->Version = tempInt;
      ...

      }

      to one which allows us to bypass writing the configuration to disk and instead pass it directly in memory, roughly equivalent to this:

      static int LoadFromString(char *Stream, ConfigStructure *cs)

      A couple of things to note: • The current LoadFromFile function is incredibly dense and complex, reading dozens of versions of the config file in a backward-compatible manner, which makes duplication of the overall logic quite a pain. • The functions that generate the config file and those that read it originate in totally different parts of the old system and therefore don't share any data structures so I can't pass those directly. I could potentially write a wrapper, but again, it would need to handle any structure passed in a backward-compatible manner. • I'm tempted to just pass the file as is in as a string (as in the prototype above) and convert all the fscanf's to sscanf's but then I have to handle incrementing the pointer along (and potentially dealing with buffer overrun errors) manually. • This has to remain in C, so no C++ functionality like streams can help here Am I missing a superior choice? Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Any pointers, ideas or other assistance is enormously valuable.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      If you are working on Linux, fmemopen[^] is your friend. There is no equivalent function under Windows but there are some emulation projects GitHub - Snaipe/fmem: A cross-platform library for opening memory-backed libc streams.[^]. Never tried it so I don’t know how well it works.

      Mircea

      1 Reply Last reply
      0
      • S sahil Ranka

        I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure:

        static int LoadFromFile(FILE *Stream, ConfigStructure *cs)
        {
        int tempInt;

        ...

        if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 )
        {
        printf("Unable to read version number\n");
        return 0;
        }
        cs->Version = tempInt;
        ...

        }

        to one which allows us to bypass writing the configuration to disk and instead pass it directly in memory, roughly equivalent to this:

        static int LoadFromString(char *Stream, ConfigStructure *cs)

        A couple of things to note: • The current LoadFromFile function is incredibly dense and complex, reading dozens of versions of the config file in a backward-compatible manner, which makes duplication of the overall logic quite a pain. • The functions that generate the config file and those that read it originate in totally different parts of the old system and therefore don't share any data structures so I can't pass those directly. I could potentially write a wrapper, but again, it would need to handle any structure passed in a backward-compatible manner. • I'm tempted to just pass the file as is in as a string (as in the prototype above) and convert all the fscanf's to sscanf's but then I have to handle incrementing the pointer along (and potentially dealing with buffer overrun errors) manually. • This has to remain in C, so no C++ functionality like streams can help here Am I missing a superior choice? Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Any pointers, ideas or other assistance is enormously valuable.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your LoadFromString method could write the string to a temporary file, open the file for input and pass the FILE* to LoadFromFile. That way you would not require any changes to the existing code.

        1 Reply Last reply
        0
        • S sahil Ranka

          I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure:

          static int LoadFromFile(FILE *Stream, ConfigStructure *cs)
          {
          int tempInt;

          ...

          if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 )
          {
          printf("Unable to read version number\n");
          return 0;
          }
          cs->Version = tempInt;
          ...

          }

          to one which allows us to bypass writing the configuration to disk and instead pass it directly in memory, roughly equivalent to this:

          static int LoadFromString(char *Stream, ConfigStructure *cs)

          A couple of things to note: • The current LoadFromFile function is incredibly dense and complex, reading dozens of versions of the config file in a backward-compatible manner, which makes duplication of the overall logic quite a pain. • The functions that generate the config file and those that read it originate in totally different parts of the old system and therefore don't share any data structures so I can't pass those directly. I could potentially write a wrapper, but again, it would need to handle any structure passed in a backward-compatible manner. • I'm tempted to just pass the file as is in as a string (as in the prototype above) and convert all the fscanf's to sscanf's but then I have to handle incrementing the pointer along (and potentially dealing with buffer overrun errors) manually. • This has to remain in C, so no C++ functionality like streams can help here Am I missing a superior choice? Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Any pointers, ideas or other assistance is enormously valuable.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          sahil Ranka wrote:

          Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk?

          Is mmap() of any value here?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups