Reading a small text file contents into an array

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

    Reading a small text file contents into an array

    Hi, another question from me again, i was just wondering if anyone
    could give me a quick example of reading data from a file then placing
    the data into an array for some manipulation then reading that array
    back into another file.

    I've tried, but i can only read the data and place it on the screen, i
    cant get it into an array.

    Any help would be helpful.

    Thanxs!
  • John Harrison

    #2
    Re: Reading a small text file contents into an array


    "Foxy Kav" <foxykav@hotmai l.com> wrote in message
    news:3257c6ed.0 404270407.22f45 49b@posting.goo gle.com...[color=blue]
    > Hi, another question from me again, i was just wondering if anyone
    > could give me a quick example of reading data from a file then placing
    > the data into an array for some manipulation then reading that array
    > back into another file.
    >
    > I've tried, but i can only read the data and place it on the screen, i
    > cant get it into an array.
    >
    > Any help would be helpful.
    >
    > Thanxs![/color]

    Really need to know what kind of data you are trying to read, chars,
    strings, ints, what?

    Also this is much better done with a vector rather than an array, because
    then you don't have to worry about the size of the array, dynamically
    resizing it etc. It makes the code a lot easier.

    john


    Comment

    • tom_usenet

      #3
      Re: Reading a small text file contents into an array

      On 27 Apr 2004 05:07:14 -0700, foxykav@hotmail .com (Foxy Kav) wrote:
      [color=blue]
      >Hi, another question from me again, i was just wondering if anyone
      >could give me a quick example of reading data from a file then placing
      >the data into an array for some manipulation then reading that array
      >back into another file.
      >
      >I've tried, but i can only read the data and place it on the screen, i
      >cant get it into an array.[/color]

      If you want the whole file in an array (or vector), you can do:

      #include <vector>
      #include <fstream>
      #include <algorithm>
      #include <iterator>

      int main()
      {
      //open the file in binary mode (to get raw chars)
      std::ifstream ifs("myfile.txt ", std::ios_base:: binary);
      //create an iterator pair over the contents of the file
      std::istreambuf _iterator begin(ifs), end;
      //copy the iterator range into a vector
      std::vector<cha r> contents(begin, end);
      //close the file
      ifs.close();

      //change a character - contents contains the whole file here
      //be careful to check how big contents is (via contents.size() ).
      contents[0] = 'q'; //or contents.at(0) = 'q'; for bounds checking

      //open file again for output
      std::ofstream ofs("myfile.txt ", std::ios_base:: binary);
      //write out whole vector in one go:
      ofs.write(&cont ents[0], contents.size() );

      //everything cleaned up automatically by destructors -
      //one of the joys of C++ over C!
      }

      Tom
      --
      C++ FAQ: http://www.parashift.com/c++-faq-lite/
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

      Comment

      • Foxy Kav

        #4
        Re: Reading a small text file contents into an array

        Uh .. thanxs for that, but im just a beginner and that makes
        absolutely no sence to me.

        Im trying to write alphanumeric charcter to a file, so char is the
        type of data i want to write to a file, and get from a file.

        Comment

        • Karl Heinz Buchegger

          #5
          Re: Reading a small text file contents into an array

          Foxy Kav wrote:[color=blue]
          >
          > Uh .. thanxs for that, but im just a beginner and that makes
          > absolutely no sence to me.
          >
          > Im trying to write alphanumeric charcter to a file, so char is the
          > type of data i want to write to a file, and get from a file.[/color]

          So what is it? Read or Write
          In your original question you asked about reading from a file. Can
          you write to a file?

          What does the file look like?
          It is very rare that one has to read from a file character per character,
          that's why I ask. Is there some structure in the file?

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • tom_usenet

            #6
            Re: Reading a small text file contents into an array

            On 28 Apr 2004 04:10:52 -0700, foxykav@hotmail .com (Foxy Kav) wrote:
            [color=blue]
            >Uh .. thanxs for that, but im just a beginner and that makes
            >absolutely no sence to me.[/color]

            Which bits didn't you understand? Or none of it? If you didn't
            understand any of it, you'll need to get a good C++ book before going
            much further. Have you used ifstream before? vector? They are both
            fairly fundamental in C++ (though neither exist in C - perhaps you are
            learning from a C book?)
            [color=blue]
            >Im trying to write alphanumeric charcter to a file, so char is the
            >type of data i want to write to a file, and get from a file.[/color]

            What have you got so far?

            Tom
            --
            C++ FAQ: http://www.parashift.com/c++-faq-lite/
            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

            Comment

            • tom_usenet

              #7
              Re: Reading a small text file contents into an array

              On Tue, 27 Apr 2004 13:52:24 +0100, tom_usenet
              <tom_usenet@hot mail.com> wrote:
              [color=blue]
              > std::istreambuf _iterator begin(ifs), end;[/color]

              That should be:

              std::istreambuf _iterator<char> begin(ifs), end;

              Tom
              --
              C++ FAQ: http://www.parashift.com/c++-faq-lite/
              C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

              Comment

              Working...