//============================================= DEMO OF FILE INPUT AND OUTPUT // We first compute some integers and floats and store them in arrays. // Next we save the arrays to a file. // Then we open that file and read the values into new different variables. // As we read the values, we render the numbers on the Window. // We use new variables to confirm that the values came from the file. #include #pragma hdrstop #include "Unit1.h" #include // YOU NEED TO TYPE THIS IN #include "math.h" // YOU NEED TO TYPE THIS IN // You need to paste in an OpenDialog component (Dialogs tab) // You need to paste in a SaveDialog component (Dialogs tab) //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //----------------------------------------------- INTEGER AND FLOAT VARIABLES // these will be used to output data when we save a file int number[10], square[10], cube[10]; double sine[10], cosine[10]; // these will be used to input data when we open a file and render it int oneNumber, oneSquare, oneCube; double oneSine, oneCosine; //--------------------------------- COMPUTE SOME INTEGERS AND FLOATS FUNCTION void computeStuff (void) { for (int i = 0; i < 10; i++ ) { number[i] = i; square[i] = i * i; cube[i] = square[i] * i; sine[i] = sin(i); cosine[i] = cos(i); } } //--------------------------------- SAVE THE STUFF THAT WE COMPUTED FUNCTION void saveStuff (void) { if (Form1->SaveDialog1->Execute()) { ofstream outfile(Form1->SaveDialog1->FileName.c_str()); for (int i = 0; i < 10; i++) { outfile << number[i] << " "; outfile << square[i] << " "; outfile << cube[i] << " "; outfile << sine[i] << " "; outfile << cosine[i] << " "; outfile << endl; } outfile.close(); } } //--------------------------------------- OPEN THE STUFF THAT WE SAVED BUTTON void openStuff (void) { if (Form1->OpenDialog1->Execute()) { ifstream infile(Form1->OpenDialog1->FileName.c_str()); for (int i = 0; i < 10; i++) { infile >> oneNumber; infile >> oneSquare; infile >> oneCube; infile >> oneSine; infile >> oneCosine; // Let's render them as we read them starting with the squares and cubes Form1->Canvas->Pen->Width = 10; Form1->Canvas->TextOut (10, oneNumber * 30 + 15, IntToStr(oneNumber)); Form1->Canvas->TextOut (10 + oneSquare, oneNumber * 30 + 15, IntToStr(oneSquare)); Form1->Canvas->TextOut (10 + oneCube , oneNumber * 30 + 15, IntToStr(oneCube)); Form1->Canvas->Pen->Color = clRed; Form1->Canvas->MoveTo(10, oneNumber * 30 + 10); Form1->Canvas->LineTo(10 + oneSquare, oneNumber * 30 + 10); Form1->Canvas->Pen->Color = clBlue; Form1->Canvas->MoveTo(10 + oneSquare, oneNumber * 30 + 10); Form1->Canvas->LineTo(10 + oneCube, oneNumber * 30 + 10); // Now let's list the Sines and Cosines Form1->Canvas->TextOut (470, oneNumber * 20 + 10, IntToStr(oneNumber)); Form1->Canvas->TextOut (500, oneNumber * 20 + 10, "Sine = " + FloatToStr(oneSine)); Form1->Canvas->TextOut (600, oneNumber * 20 + 10, "Cosine = " + FloatToStr(oneCosine)); } infile.close(); } } //------------------------------------------------------ COMPUTE STUFF BUTTON void __fastcall TForm1::ButtonComputeClick(TObject *Sender) { computeStuff(); } //------------------------------------ SAVE THE STUFF THAT WE COMPUTED BUTTON void __fastcall TForm1::ButtonSaveClick(TObject *Sender) { saveStuff(); } //--------------------------------------- OPEN THE STUFF THAT WE SAVED BUTTON void __fastcall TForm1::ButtonOpenClick(TObject *Sender) { openStuff(); } //---------------------------------------------------------------------------