Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Created January 18, 2020 00:04
Show Gist options
  • Save Theartbug/d4df367b5f38a4f6f38f2a7f1646f026 to your computer and use it in GitHub Desktop.
Save Theartbug/d4df367b5f38a4f6f38f2a7f1646f026 to your computer and use it in GitHub Desktop.
C++ external files

External Files

  • a way to persist data on a hard drive
    • where files are stored
  • usually programs utilize RAM (main memory) to execute
    • when data is taken in from the user
    • where variables are stored
  • output is when we write to a file
    • ofstream
  • input is when we read from a file
    • ifstream
  • #include <fstream>
    • where input and output stream come from
  • both can be used to attach to a file
    • read char by char when outputting
#include <fstream>
using namespace std;

ifstream in;
ofstream out;

in.open("filename.txt"); // connects to a file
out.open("filename.txt"); // can pass in a relative or absolute path

if(in) // check that the connection was successful
{
	// MUST attempt to read in prior to the while loop
	// in order to trigger the .eof() flag
	in.get(struct.array, ARRAY_SIZE, '\n'); // stop at the delimiter
	in.ignore(100, '\n') // skips over the delimiter that we stopped at
	while(in && !in.eof()) // while we exist and have no reached the end of the file
	{
		in.get(struct.name, NAME_SIZE, '\n');
		...
	}
	in.close(); // close ifstream access to the file, releases holds
} else cout << "Error" << endl;
if(out)
{
	out << struct.array << '\n';
	out << struct.name << '\n';
	out.close(); // close hold on file
	out.clear(); // clear out any stuff in buffer?
}
  • will typically only reference files that are within root
    • can call by name directly without path
  • can pass in a string or array of characters for opening
  • if attempt to open a file for output (ofstream)
    • does not have to exist, will create the file
    • if it does exist, will overwrite it
    • unless the file CANNOT be created, will error by returning 0
      • but do not need to clear error in the stream
  • if attempt to open a file for input (ifstream)
    • will fail if file does not exist
  • out.open(filename, ios::app)
    • when you want to open a file to add to it instead of overwriting it (default behavior)
    • will append to the end automatically
  • when writing to a file, make sure to add a known delimiter to differentiate between different data additions
    • entry1, entry2, entry3, entry4 the deliminator is ,
  • when reading from a file, ifstream will position the cursor at the start of the file
    • can choose where the cursor is placed
  • when reading in from ifstream
    • >> will skip whitespace for character arrays and integers
    • .get() includes whitespace
    • .get(variable, size, deliminator) for more fine-tuning
  • in.eof() a flag that gets set when the end of file is reached
    • must first attempt a read to set the flag (must fail first)
  • ALWAYS make a read prior to a loop and checking .eof()
for(char ch = in.get(); in && !in.eof(); ch = in.get()) { ... };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment