Reading a small text file contents into an array

F

Foxy Kav

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!
 
J

John Harrison

Foxy Kav said:
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!

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
 
T

tom_usenet

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.

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::eek:fstream 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
 
F

Foxy Kav

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.
 
K

Karl Heinz Buchegger

Foxy said:
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.

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?
 
T

tom_usenet

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

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?)
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.

What have you got so far?

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top