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. STL / elegant file input (basic question!)

STL / elegant file input (basic question!)

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++performance
9 Posts 3 Posters 0 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.
  • L Offline
    L Offline
    LiquidEyes
    wrote on last edited by
    #1

    At the moment I'm trying to make an effort to use std::string and std::ifstream in my work, instead of using "stdio.h". My main objective is to write elegant code -- performance is not an issue! :) I need to read a lot of blocks (of various sizes) from file, and append them to various string buffers. The problem is that functions like ifstream::read are designed to read into char* style buffers, not strings; however there seems to be some kind of relationship between streambufs and strings that I figured might make my life easier...? :confused:using namespace std; ... string mystring; ifstream inputfile; int length = 20; ... _// is there a better way to do this...?_ char* buffer = new char [length]; inputfile.read (buffer, length); mystring += buffer; delete[] buffer; _// this isn't much better..._ char ch; for (int i=0; i<length; i++) { inputfile >> ch; mystring += ch; _// presumably I could **reserve** extra space before the loop,_ _// to avoid repeated resizing of the string...?_ }
    I have the gut feeling that there must be a really elegant way to do this, using some combination of streambuf / ifstream::operator>> / ifstream::read etc. I figured the whole point of using std::string was that it can worry about the allocation side of things behind the scenes. However I'm finding the documentation a little cryptic and generalised. :~ Any ideas? It's quite possible I just want the moon on a stick and I should go back to BASIC... :)

    R 1 Reply Last reply
    0
    • L LiquidEyes

      At the moment I'm trying to make an effort to use std::string and std::ifstream in my work, instead of using "stdio.h". My main objective is to write elegant code -- performance is not an issue! :) I need to read a lot of blocks (of various sizes) from file, and append them to various string buffers. The problem is that functions like ifstream::read are designed to read into char* style buffers, not strings; however there seems to be some kind of relationship between streambufs and strings that I figured might make my life easier...? :confused:using namespace std; ... string mystring; ifstream inputfile; int length = 20; ... _// is there a better way to do this...?_ char* buffer = new char [length]; inputfile.read (buffer, length); mystring += buffer; delete[] buffer; _// this isn't much better..._ char ch; for (int i=0; i<length; i++) { inputfile >> ch; mystring += ch; _// presumably I could **reserve** extra space before the loop,_ _// to avoid repeated resizing of the string...?_ }
      I have the gut feeling that there must be a really elegant way to do this, using some combination of streambuf / ifstream::operator>> / ifstream::read etc. I figured the whole point of using std::string was that it can worry about the allocation side of things behind the scenes. However I'm finding the documentation a little cryptic and generalised. :~ Any ideas? It's quite possible I just want the moon on a stick and I should go back to BASIC... :)

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      LiquidEyes wrote: Any ideas? getline(inputfile, mystring); will read a string from a file. You can pass a third parameter (a character) that specifies a delimiter to use during the reading if you need to. By default, it stops after a newline character.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      L 1 Reply Last reply
      0
      • R Ryan Binns

        LiquidEyes wrote: Any ideas? getline(inputfile, mystring); will read a string from a file. You can pass a third parameter (a character) that specifies a delimiter to use during the reading if you need to. By default, it stops after a newline character.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        L Offline
        L Offline
        LiquidEyes
        wrote on last edited by
        #3

        Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of fread( mystring, sizeof(char), length, inputfile ); or failing that, use something more elegant than a char[] (which I have to allocate myself) as an intermediate buffer.

        K R 2 Replies Last reply
        0
        • L LiquidEyes

          Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of fread( mystring, sizeof(char), length, inputfile ); or failing that, use something more elegant than a char[] (which I have to allocate myself) as an intermediate buffer.

          K Offline
          K Offline
          Kevin McFarlane
          wrote on last edited by
          #4

          From memory I think you can do getline(infile, mystring, "\n") or something. Kevin

          L 1 Reply Last reply
          0
          • K Kevin McFarlane

            From memory I think you can do getline(infile, mystring, "\n") or something. Kevin

            L Offline
            L Offline
            LiquidEyes
            wrote on last edited by
            #5

            I don't want it to stop on 'newline'. I want it to read a fixed number of bytes. :confused:

            K 1 Reply Last reply
            0
            • L LiquidEyes

              Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string? I want to do the STL equivalent of fread( mystring, sizeof(char), length, inputfile ); or failing that, use something more elegant than a char[] (which I have to allocate myself) as an intermediate buffer.

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              LiquidEyes wrote: However I don't see how it will know to stop after (say) 20 characters? It doesn't. You'll have to use the read() method of ifstream to do that. LiquidEyes wrote: Without a delimeter, will it read the whole file into the string? No, as I said, the default delimiter is a newline - it will read a single line.

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              L 1 Reply Last reply
              0
              • R Ryan Binns

                LiquidEyes wrote: However I don't see how it will know to stop after (say) 20 characters? It doesn't. You'll have to use the read() method of ifstream to do that. LiquidEyes wrote: Without a delimeter, will it read the whole file into the string? No, as I said, the default delimiter is a newline - it will read a single line.

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                L Offline
                L Offline
                LiquidEyes
                wrote on last edited by
                #7

                You'll have to use the read() method of ifstream to do that. Precisely. Which takes us back to my original question ... what structure can I read into instead of an old fashioned char* array? I just thought there might be an elegant way, using vectors or strings or something, that didn't involve allocating chars manually. As I said, maybe I just want the moon on a stick. :)

                R 1 Reply Last reply
                0
                • L LiquidEyes

                  You'll have to use the read() method of ifstream to do that. Precisely. Which takes us back to my original question ... what structure can I read into instead of an old fashioned char* array? I just thought there might be an elegant way, using vectors or strings or something, that didn't involve allocating chars manually. As I said, maybe I just want the moon on a stick. :)

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  LiquidEyes wrote: what structure can I read into instead of an old fashioned char* array? Just quickly looking at the docs, it appears you can use ifstream::getline(mystring, number_of_chars). If you pass a size of, say, 10, it reads 9 characters and appends a NULL. Once again, you can add a third parameter which is the delimiter if you need it.

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  1 Reply Last reply
                  0
                  • L LiquidEyes

                    I don't want it to stop on 'newline'. I want it to read a fixed number of bytes. :confused:

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #9

                    I couldn't find that getline with no. of bytes that Ray mentioned. I suppose you could use the getline that gets the whole line and then use a substring function. Not as elegant but still avoids char*. Kevin

                    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