A more efficient way for file I/O in C++?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

A more efficient way for file I/O in C++?

Post by davidthefat »

I think fstream and ofstream and ifsteam is decent but what if you have to load more that a dozen line of text like a journal or something. for example, a file with data of the weights i used. what im asking is that can the functions be used with multidimensional arrays?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: A more efficient way for file I/O in C++?

Post by Falco Girgis »

Yeah, of course ifstream can read into multidimensional arrays:

Code: Select all

ifstream file(filename);
file >> variable[x][y];
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: A more efficient way for file I/O in C++?

Post by MarauderIIC »

You can even be like awesome and

Code: Select all

vector <int> nums;
int num;
file >> num;
while (file) {
    nums.push_back(num);
    file >> num;
}
or

Code: Select all

int array[20zillion];
for (int i = 0; file && i != 20zillion; ++i) { //lolololol
    file >> array[i];
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: A more efficient way for file I/O in C++?

Post by avansc »

people dont rely on plain text, make binary files and just read entire stuctures/classes at a time, alot faster than processing each line or what not.
i think C++ is making developers lazy and ultimatly stupid. (no offence to anyone.)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply