Sign in to follow this  
Followers 0
  • entries
    23
  • comments
    30
  • views
    54195

C++ File Read Write Save Delete Example

Sign in to follow this  
Followers 0
RLS0812

1508 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"); }}

0
Sign in to follow this  
Followers 0


2 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.

0

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.

1

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