Sign in to follow this  
  • entries
    23
  • comments
    31
  • views
    54789

C++ File Read Write Save Delete Example

Sign in to follow this  
RLS0812

1666 views

A simplified example of how to work with files in C++ .
.// Create File, Write To File// Close File// Open File, Dump Text To Aray// Close File// Delete File#include #include #include using namespace std;int main(){ string str[1]; ofstream a_file ("Ftest.txt"); a_file<<"File Test"; a_file.close();string line[1]; ifstream b_file ("Ftest.txt"); if (b_file.is_open()) { getline (b_file,line[0]); cout << line << endl; b_file.close(); remove("Ftest.txt"); }}
Sign in to follow this  


2 Comments


Recommended Comments

It'd be simpler still if there weren't one-element arrays, which are confusing to someone that thinks they're there for a reason.

Moreover, str seems to go unused.

Share this comment


Link to comment

With respect, that is a dreadful example:

 

1) As has been pointed out, you are using arrays of variables pointlessly - a simple variable will suffice

2) You aren't including the correct header for "remove" (cstdlib I think, or maybe cstdio)

3) Your comments are misleading - nowhere do I see an array (Aray) being used

4) You aren't checking error states anywhere - this is important with files

 

I'll be nice and blame the terrible formatting on GDNet's code editor.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now