Hello,

I'm fairly new to C++, and i would like to know how to make a program that uses IO stream? like, i want it to have settings feature where the user would enter thier info on a form, and it saves as a .txt or another format file, and it loads everytime the program starts. I tried this before on my old laptop, it returned with some error.

How do i tell the program to get this information from X line? should i use EOF function? and someone can please remind me how to do that, that would be awesome.

Thanks for the trouble and helping.

Regards,
Mike.

Yes sir. I forgot to mention that im using Microsoft Visual C++ Express 2010. I was using Dev C++, but i wanted to try something new? Thanks

all EOF does is checks to see whether or not the end of the file was reached. Usually this is used when doing a loop in a which you read input from the file.

By what you described, it sounds like the user should be prompted with a menu to either change their information or to continue to the program. In that case, choice 1 the program would send the use to a void function where the program runs the input info to a text file.

Here is an example:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

Then do the opposite when the user just wants to continue to the program where the program reads the file. In that case you would do an ifstream rather than an ofstream. Again, for the program to remember what each line means, i would use an array and make sure the program understands which index from the file means what. I'm not going to go about tell you how to do this exactly but if you read a little bit on the link that Labdabeta posted you should be able to learn what to do quickly.

If you make a WinForms app and add using statements for:

using namespace System::Collections::Generic;
using namespace System::IO;

You can then create functions to do what you mentioned.
You will need to add the fields on the form and make the code pick up the data and write it to an output file.
My test form contains three fields Name, Rank, Phone with the IDs of the text boxes respectively set to tbxName, tbxRank, tbxPhone.
I disable the Save button until something modifies the text boxes. I then enable it, but disable it after save.

You will need to make a handler for each of the text boxes, so you know when they change.

The file is in the current directory (not a good practice) and is read on program load.
The file is overwitten when the user clicks the Save.

Let me know if you need any of this explained further.

Here are some functions that are used to make this happen:

   private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
   {
      try
      {
         List<String^>^ lst_strData = gcnew List<String^>();
         StreamReader^ fileIn = gcnew StreamReader("Data.txt");
         while(!fileIn->EndOfStream)
         {
            lst_strData->Add(fileIn->ReadLine());
         }
         fileIn->Close();

         if(lst_strData->Count < 3)
         {
            return;
         }

         tbxName->Text = lst_strData[0];
         tbxRank->Text = lst_strData[1];
         tbxPhone->Text = lst_strData[2];
      }
      catch(Exception^ exc)
      {
         MessageBox::Show(exc->Message);
      }

      bnSave->Enabled = false;
   }
private: System::Void bnSave_Click(System::Object^  sender, System::EventArgs^  e)
   {
      try
      {
         StreamWriter^ fileOut = gcnew StreamWriter("Data.txt");

         fileOut->WriteLine(tbxName->Text);
         fileOut->WriteLine(tbxRank->Text);
         fileOut->WriteLine(tbxPhone->Text);

         fileOut->Close();
      }
      catch(Exception^ exc)
      {
         MessageBox::Show(exc->Message);
      }

      bnSave->Enabled = false;
   }

   private: System::Void tbxName_TextChanged(System::Object^  sender, System::EventArgs^  e)
   {
      bnSave->Enabled=true;
   }
   private: System::Void tbxRank_TextChanged(System::Object^  sender, System::EventArgs^  e)
   {
      bnSave->Enabled=true;
   }
   private: System::Void tbxPhone_TextChanged(System::Object^  sender, System::EventArgs^  e)
   {
      bnSave->Enabled=true;
   }

Edited 4 Years Ago by thines01: added image

Comments
Best guy to get help from xD
Attachments DW_419296.jpg 15.44 KB

I thank you very much sir for giving me an example. I didn't know i had to make a handle for each textboxes, neither how to make one. Sleep tonight knowing you helped a very humble person. Sorry for posting late also.

OK. Good.
To create the handler for the textboxes, just double-click them in the design view and the editor will generate the code.

This article has been dead for over six months. Start a new discussion instead.