Friday, February 1, 2008

FINALLY I get to work on C++

Well somehow I managed to miss all classes this week except Monday. Too bad, but today the ski meet ended early and now I can PROGRAM! I've been experimenting with file I/O because whether or not I ever figure out windows, I should probably learn this. On Monday I'm going to look through the books to try to get one on Windows programming. That will be much easier than trying to read these tutorials.

I used the header "fstream" which included ifstream and ofstream to input and output to files to create a program that creates a file with some words in it. To create the file (or open it) I use the lines:

ofstream fout; //fout is a handle, I think
fout.open("file.txt"); //opens file.txt with the fout handle thing

To output to the file the line is:

fout << "word"; //pretty simple, right?

I've messed around a bit with reading from files too. If I open a file with "ifstream" instead of "ofstream" (ifstream = i-f-stream = input-file-stream) I have the ability to read from the file. I can then input the text to a string. The problem here is that the input line looks like this:

fin >> string; //problem = too simple... how can I specify what to get?

So now we have the text inside of the string, and it reads all the text up to any blank spots (spaces or returns). If I put:

fin >> string;
fin >> string;

It inputs up to the blank spot, then inputs whatever is past the blank spot and overwrites the first thing. Therefore if the file contained "a b" string would contain "b".

That's about as far as I've gotten so far... but I have about a weeks time to make up so there's much more to come!

No comments: