Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

C++ File Read Write Save Delete Example

Sign in to follow this  


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  


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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!