Connecting Tech Pros Worldwide Help | Site Map

Reading a small text file contents into an array

Foxy Kav
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Reading a small text file contents into an array



"Foxy Kav" <foxykav@hotmail.com> wrote in message
news:3257c6ed.0404270407.22f4549b@posting.google.c om...[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


tom_usenet
Guest
 
Posts: n/a
#3: Jul 22 '05

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<char> 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(&contents[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
Foxy Kav
Guest
 
Posts: n/a
#4: Jul 22 '05

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.
Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 22 '05

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
tom_usenet
Guest
 
Posts: n/a
#6: Jul 22 '05

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
tom_usenet
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Reading a small text file contents into an array


On Tue, 27 Apr 2004 13:52:24 +0100, tom_usenet
<tom_usenet@hotmail.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
Closed Thread