Jump to content

- - - - -

C++ File Read Write Save Delete Example

C++ file i/o io
4: Adsense

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 <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  string str[1];
  ofstream a_file ("Ftest.txt");
  a_file<<"File Test";
string line[1];
  ifstream b_file ("Ftest.txt");
  if (b_file.is_open())
      getline (b_file,line[0]);
      cout << line << endl;

Mar 04 2014 10:27 AM

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.

Mar 04 2014 12:37 PM

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.

Note: GameDev.net moderates comments.