C++ Source Code

  • Work-from-home

Mahen

Alhamdulillah
VIP
Jun 9, 2012
21,845
16,877
1,313
laнore
C++ source code ... a program which uses class to read and write to a txt file


Code:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
 
using namespace std;
 
class FileHandler
{
      private:
            fstream file;
            string Data;
      public:
            void SetData(string a)
            {
                  Data=a;
            }
            void WriteToFile()
            {
                          file.open("file.txt",ios::out);
                          file<<Data;
                          file.close();
            }
            void ReadFromFile()
            {
                          file.open("file.txt",ios::in);
                          getline(file,Data);
                          cout<<"The Data was : " <<Data<<endl;
            }
};
 
 
int main()
{
    FileHandler f;
    f.SetData("This text should be written on file, if the source code is correct");
    f.WriteToFile();
    f.ReadFromFile();
    system("pause");
}
koi mistake hai tou thik kar do {(popcorn)} mujy na khna :p @Don @ChoCo
 

ChoCo

{ ' . ' } {C__C}
Super Star
Oct 29, 2012
6,169
6,637
363
MaRs
C++ source code ... a program which uses class to read and write to a txt file


Code:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
 
using namespace std;
 
class FileHandler
{
      private:
            fstream file;
            string Data;
      public:
            void SetData(string a)
            {
                  Data=a;
            }
            void WriteToFile()
            {
                          file.open("file.txt",ios::out);
                          file<<Data;
                          file.close();
            }
            void ReadFromFile()
            {
                          file.open("file.txt",ios::in);
                          getline(file,Data);
                          cout<<"The Data was : " <<Data<<endl;
            }
};
 
 
int main()
{
    FileHandler f;
    f.SetData("This text should be written on file, if the source code is correct");
    f.WriteToFile();
    f.ReadFromFile();
    system("pause");
}
koi mistake hai tou thik kar do {(popcorn)} mujy na khna :p @Don @ChoCo
you get 10/10 for this code {(queen)}
 
  • Like
Reactions: Mahen

Don

Administrator
Mar 15, 2007
11,035
14,651
1,313
Toronto, Ca
C++ source code ... a program which uses class to read and write to a txt file


Code:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
 
using namespace std;
 
class FileHandler
{
      private:
            fstream file;
            string Data;
      public:
            void SetData(string a)
            {
                  Data=a;
            }
            void WriteToFile()
            {
                          file.open("file.txt",ios::out);
                          file<<Data;
                          file.close();
            }
            void ReadFromFile()
            {
                          file.open("file.txt",ios::in);
                          getline(file,Data);
                          cout<<"The Data was : " <<Data<<endl;
            }
};
 
 
int main()
{
    FileHandler f;
    f.SetData("This text should be written on file, if the source code is correct");
    f.WriteToFile();
    f.ReadFromFile();
    system("pause");
}
koi mistake hai tou thik kar do {(popcorn)} mujy na khna :p @Don @ChoCo
good, but can you explain your code ?
 
  • Like
Reactions: Mahen and ChoCo

ChoCo

{ ' . ' } {C__C}
Super Star
Oct 29, 2012
6,169
6,637
363
MaRs
good, but can you explain your code ?
Ahh let me do it for her don :)[DOUBLEPOST=1360999223][/DOUBLEPOST]
Code:
#include <iostream.h> \\ adds input output commands header file
 
#include <string.h> \\ adds input stirng header file
 
#include <fstream.h> \\ forgot about that
 
using namespace std; \\ uses standard name space etc :)
 
class FileHandler \\ she is making a new class of her own named filehandler
 
{
 
private: \\ function called private
 
fstream file; \\ fetches the file
 
string Data; \\ declares data as string
 
public: \\ function called public
 
void SetData(string a) \\ she is parsing values to data :p
 
{
 
Data=a; \\ assigns data some input suppose a :) that is to be written in file
 
}
 
void WriteToFile() \\ again parsing values without arguments
 
{
 
file.open("file.txt",ios::eek:ut); \\ she opens the file
 
file<<Data; \\ writes her data
 
file.close(); \\ closes the file
 
}
 
void ReadFromFile() \\ now for reading data parsing values without arguments
 
{
 
file.open("file.txt",ios::in); \\ opens it
 
getline(file,Data); \\ gets data from specific line and file
 
cout<<"The Data was : " <<Data<<endl; \\ displays on screen
 
}
 
};
 
int main() \\ this is the main function
 
{
 
FileHandler f; \\ calls out file
 
f.SetData("This text should be written on file, if the source code is correct"); \\ she is giving comments
 
f.WriteToFile(); \\ calling functions
 
f.ReadFromFile(); \\ calling functions
 
system("pause"); \\ halting system
 
}
[DOUBLEPOST=1360999252][/DOUBLEPOST]@Don
 
  • Like
Reactions: Mahen

Mahen

Alhamdulillah
VIP
Jun 9, 2012
21,845
16,877
1,313
laнore
good, but can you explain your code ?
nope.!!!{(popcorn)} :p[DOUBLEPOST=1360999412][/DOUBLEPOST]
Ahh let me do it for her don :)[DOUBLEPOST=1360999223][/DOUBLEPOST]#include <iostream.h> \\ adds input output commands header file
#include <string.h> \\ adds input stirng header file
#include <fstream.h> \\ forgot about that

using namespace std; \\ uses standard name space etc :)

class FileHandler \\ she is making a new class of her own named filehandler
{
private: \\ function called private
fstream file; \\ fetches the file
string Data; \\ declares data as string
public: \\ function called public
void SetData(string a) \\ she is parsing values to data :p
{
Data=a; \\ assigns data some input suppose a :) that is to be written in file
}
void WriteToFile() \\ again parsing values without arguments
{
file.open("file.txt",ios::eek:ut); \\ she opens the file
file<<Data; \\ writes her data
file.close(); \\ closes the file
}
void ReadFromFile() \\ now for reading data parsing values without arguments
{
file.open("file.txt",ios::in); \\ opens it
getline(file,Data); \\ gets data from specific line and file
cout<<"The Data was : " <<Data<<endl; \\ displays on screen
}
};


int main() \\ this is the main function
{
FileHandler f; \\ calls out file
f.SetData("This text should be written on file, if the source code is correct"); \\ she is giving comments
f.WriteToFile(); \\ calling functions
f.ReadFromFile(); \\ calling functions
system("pause"); \\ halting system
}[DOUBLEPOST=1360999252][/DOUBLEPOST]@Don
Ahan tum ny tou waqi kar bhi diya :-bd mujy zara teng tou krny datye :eek: :p
wasie aj kisi ki rat zara jaldi nhi ho gie :p @ChoCo {}{78iu
 
  • Like
Reactions: ChoCo

Don

Administrator
Mar 15, 2007
11,035
14,651
1,313
Toronto, Ca
Ahh let me do it for her don :)[DOUBLEPOST=1360999223][/DOUBLEPOST]
Code:
#include <iostream.h> \\ adds input output commands header file
 
#include <string.h> \\ adds input stirng header file
 
#include <fstream.h> \\ forgot about that
 
using namespace std; \\ uses standard name space etc :)
 
class FileHandler \\ she is making a new class of her own named filehandler
 
{
 
private: \\ function called private
 
fstream file; \\ fetches the file
 
string Data; \\ declares data as string
 
public: \\ function called public
 
void SetData(string a) \\ she is parsing values to data :p
 
{
 
Data=a; \\ assigns data some input suppose a :) that is to be written in file
 
}
 
void WriteToFile() \\ again parsing values without arguments
 
{
 
file.open("file.txt",ios::eek:ut); \\ she opens the file
 
file<<Data; \\ writes her data
 
file.close(); \\ closes the file
 
}
 
void ReadFromFile() \\ now for reading data parsing values without arguments
 
{
 
file.open("file.txt",ios::in); \\ opens it
 
getline(file,Data); \\ gets data from specific line and file
 
cout<<"The Data was : " <<Data<<endl; \\ displays on screen
 
}
 
};
 
int main() \\ this is the main function
 
{
 
FileHandler f; \\ calls out file
 
f.SetData("This text should be written on file, if the source code is correct"); \\ she is giving comments
 
f.WriteToFile(); \\ calling functions
 
f.ReadFromFile(); \\ calling functions
 
system("pause"); \\ halting system
 
}
[DOUBLEPOST=1360999252][/DOUBLEPOST]@Don
thank you... and good job :)
 
  • Like
Reactions: ChoCo
Top