fstream Checking if file exists c++ - c++

Hi guys I am working on an rpg project and I am creating player files so they can save their progress and such.
I've made a test program so I can show you on a more simple scale of what I am looking for
Code:
#include <iostream>
#include <fstream>
#include <string>
int main(){
std::string PlayerFileName;
std::cout << "Name Your Player File Name: ";
std::cin >> PlayerFileName;
std::ofstream outputFile;
std::string FileName = "Players/" + PlayerFileName;
outputFile.open(FileName); // This creates the file
// ...
}
I want to check and see if the Player File Name already exists the the Players directory so people cant save over their progress.
Thanks!

I suggest opening the file in binary mode and using seekg() and tellg() to count it's size. If the size is bigger than 0 bytes this means that the file has been opened before and has data written in it:
void checkFile()
{
long checkBytes;
myFile.open(fileName, ios::in | ios::out | ios::binary);
if (!myFile)
{
cout << "\n Error opening file.";
exit(1);
}
myFile.seekg(0, ios::end); // put pointer at end of file
checkBytes = myFile.tellg(); // get file size in bytes, store it in variable "checkBytes";
if (checkBytes > 0) // if file size is bigger than 0 bytes
{
cout << "\n File already exists and has data written in it;
myFile.close();
}
else
{
myFile.seekg(0. ios::beg); // put pointer back at beginning
// write your code here
}
}

Check if file exists like this:
inline bool exists (const std::string& filename) {
struct stat buffer;
return (stat (filename.c_str(), &buffer) == 0);
}
Using this needs to remember to #include <sys/stat.h>.
-
In C++14 it is possible to use this:
#include <experimental/filesystem>
bool exist = std::experimental::filesystem::exists(filename);
& in C++17: (reference)
#include <filesystem>
bool exist = std::filesystem::exists(filename);

Related

How to read in binary mode and transfer the information to new file

i tried to open and read an application in binary mode and get 100 characters(For a large file, i did this so that i could read all the characters) in binary mode and then transfer them to new file(in fact, this program will be the same as the previous program, but with a different name ) to find out if it works properly or not
so anyway my source code is:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
//Vector of 100 characters initialised to 0
vector<char> ch(100, 0);
ifstream file("example.exe",ios::in|ios::binary);
if (file.is_open())
{
while (file)
{
file.read(ch.data(), 100);
// Get the number of bytes actually read
size_t count = file.gcount();
ch[file.gcount()] = '\0';
//cout << ch.data() << endl;
ofstream output("output.exe", ios::out | ios::binary | ios::app);
output.write(ch.data(), sizeof(100));
output.close();
}
}
file.close();
}
my problem is the output with the correct information is not included and the size is smaller than the original application(example.exe) what is happening?
The line output.write(ch.data(), sizeof(100));
should be replaced with output.write(ch.data(), count);
Since sizeof(100) only returns 4, which is the size in bytes of an integer.
You should also remove the line ch[file.gcount()] = '\0'; since file.gcount() might be out of bound.
I just tested it and this works for me
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
vector<char> ch(100, 0);
ifstream file("app.exe",ios::in|ios::binary);
if (file.is_open())
{
ofstream output("app_copy.exe", ios::out | ios::binary | ios::trunc);
while (file)
{
file.read(ch.data(), 100);
output.write(ch.data(), file.gcount());
}
output.close();
}
file.close();
}
Notice that ios::app is replaced by ios::trunc, which will delete the content of the file when opening.

How to use a single fstream for creation, reading and writing a file

I would like to access a file through fstream with the following requirements:
If the files does not exists, it create it
The file can be read (from pos 0)
The file can be (over)written (from pos 0)
Without closing and re-opening the file
ios_base::in seems to disable file creation
ios_base::out seems to disable file reading
Is this possible? How?
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;
int main()
{
auto mode = ios_base::in|ios_base::out;
std::string filePath = "./test.txt";
std::string content1 = "Any content 1";
std::string content2 = "Any content 2";
{
std::remove(filePath.c_str());
}
{// Test creation
// make sure test.txt is missing / does not exists
fstream file(filePath, mode);
assert(file.is_open() && file.good());
file << content1;
}
{ // Test reading & writing
fstream file(filePath, mode);
// read
file.seekg(0);
std::stringstream buffer1;
buffer1 << file.rdbuf();
cout << buffer1.str() << endl;
assert(buffer1.str()==content1);
// write
file.seekp(0);
file << content2;
assert(file.is_open() && file.good());
// read
file.seekg(0);
std::stringstream buffer2;
buffer2 << file.rdbuf();
cout << buffer2.str() << endl;
assert(buffer2.str()==content2);
}
return 0;
}
Run it
Only with fstream I'd say no.
You might want to have something similar with the trunc mode but you'll lose everything if the file exists (which might be a problem, if not go for trunc + out)
The other way is to check if the file exists, if not you create it (whichever way). Then you open with In and Out and do your stuff.
It kind of doesn't make sense to be able to read inside an empty file you just created from the cpp point of view

c++ fstream not writing to object properly

I have the following code:
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include "Person.h"
using namespace std;
int main()
{
// ========================================================================================
// part a - initialiaze 100 records, lastName = "unassigned", firstName = "", age = 0
ofstream outPerson_0("nameage.dat", ios::out | ios::binary);
// exit program if ofstream could not open file
if (!outPerson_0)
{
cerr << "File could not be opened." << endl;
exit(EXIT_FAILURE);
} // end if
Person blankPerson("unassigned", "", 0); // constructor zeros out each data member
// output 100 blank records to file
for (int i = 0; i < 100; ++i)
outPerson_0.write(reinterpret_cast< const char * >(&blankPerson), sizeof(Person));
// ========================================================================================
// part b - input 10 first names and ages and write them to file
string firstName;
string lastName;
int age;
fstream outPerson_1("nameage.dat", ios::in | ios::out | ios::binary);
// exit program if fstream cannot open file
if (!outPerson_1)
{
cerr << "File could not be opened." << endl;
exit(EXIT_FAILURE);
} // end if
// iterate 10 times to get first names and ages
for (int i = 0; i < 10; ++i)
{
// get user input
cout << "Enter last name, first name and age (space separated): ";
// set dummy values in object
std::string s = std::to_string(i);
blankPerson.setLastName(s);
blankPerson.setFirstName(s);
blankPerson.setAge(i);
// seek position in file of user-specified record, using i
auto pos = (i) * sizeof(Person);
outPerson_1.seekp(pos);
// write user-specified information in file
outPerson_1.write(reinterpret_cast<const char *>(&blankPerson), sizeof(Person));
}
// ========================================================================================
// part c - update record with no info
}
Part a populated 100 objects with dummy values, and writes to file. Part b updates the first 10 objects with dummy values generated by iterator i. For some strange reason, part b does not work properly. Could someone tell me why? The result is exactly the same as when I run part a alone.
Normally object is not suggested to write to file directly, but just by case, your code has other problem:
the ofstream and fstream instances have its own internal buffer, the data will be written into internal buffer before flushing/syncing to underlying file.
Before quitting the program,
fstream outPerson_1 will destroyed
its internal buffer will be flushed to file "nameage.dat"
ofstream outPerson_0 is destroyed
its internal buffer will be flushed to file "nameage.dat" again
(as for why, it is another topic, don't talk here)
Since the data in outPerson_0 is more than the data in outPerson_1, so the outPerson_1 data is overwritten, and then you can't see it, if you write more data into outPerson_1, you will observe it.
There may be 2 options for your code:
Option#1:
flush the data after writing the first 100 person data
// output 100 blank records to file
for (int i = 0; i < 100; ++i)
outPerson_0.write(reinterpret_cast< const char * >(&blankPerson), sizeof(Person));
outPerson_0.flush();
Option#2:
Add ios::app flag to let ofstream/fstream to append the data to the file
ofstream outPerson_0("nameage.dat", ios::out | ios::binary | ios::app);
fstream outPerson_1("nameage.dat", ios::in | ios::out | ios::binary | ios::app);

Determine the size of a binary file

I'm trying to read a binary file and I need to determine its size, but regardless of the method I've tried, I'm getting a size of zero.
For example:
fstream cbf(address, ios::binary | ios::in | ios::ate);
fstream::pos_type size = cbf.tellg(); // Returns 0.
char* chunk = new char[size];
cbf.read(chunk, size);
//...
If I were to use the following:
#include <sys/stat.h>
struct stat st;
stat(address.c_str(),&st);
int size = st.st_size;
The size is still zero. I've also tried the following, but it's still zero.
File* fp;
fp = open(address.c_str(), "rb");
How do I get the size of the file?
Thanks for the responses... I've identified the problem:
The binary file I was trying to access was created during the execution, and I just had forgotten to close it before trying to read from it...
Neither of your examples checks for failure. This program, using your first method, works perfectly well for me. It correctly identifies the size of /etc/passwd and the non-existence of /etc/motd.
#include <fstream>
#include <iostream>
#include <string>
void printSize(const std::string& address) {
std::fstream motd(address.c_str(), std::ios::binary|std::ios::in|std::ios::ate);
if(motd) {
std::fstream::pos_type size = motd.tellg();
std::cout << address << " " << size << "\n";
} else {
perror(address.c_str());
}
}
int main () {
printSize("/etc/motd");
printSize("/etc/passwd");
}
Try to load the file in this method.
Note: Use ifstream insted of fstream in this line ifstream cbf(address, ios::binary | ios::in );
long size;
ifstream cbf(address, ios::binary | ios::in);
cbf.seekg(0, ios::end);
size=cbf.tellg();
cbf.seekg(0, ios::beg);
char* chunk = new char[size];
cbf.read(chunk, size);

Write to the middle of an existing binary file c++

I'm trying to open a binary file for writing without erasing the content. But I do not want to write to eof. I want to write to a specific position in file.
Here is a litte example:
ofstream out("test.txt", ios::binary | ios::app);
for(int i = 0; i < 100; i++)
out.put('_');
out.write("Hallo", 5);
out.close();
ofstream out2("test.txt", ios::binary | ios::app);
out2.seekp(10);
out2.write("Welt", 4);
out2.close();
If using app, seek doesn't work. If not using app opening file erases data. Does anybody know an answer?
try the second overload of seekp, which allows you to provide an offset and a direction, this could be begining of file in your case (i.e. ios_base::beg). This of course assumes you know what you are doing and all you want to do is overwrite an existing number of characters.
EDIT: here is fully working example:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
{
ofstream out("test.txt", ios::binary);
for(int i = 0; i < 100; i++)
out.put('_');
out.write("Hallo", 5);
}
{
fstream out2("test.txt", ios::binary | ios::out | ios::in);
out2.seekp(10, ios::beg);
out2.write("Welt", 4);
}
}
When opening with ios::app, it is as if you open a new file that just happened to be attached to an existing file: you can not access the existing file. I'm not sure, because I would do as in Kerrek's answer, but if you really want to try, you probably have to open with "ios::in | ios::out", similar to fopen("test.txt", "rw").
Or as crashmstr points out: ios::out might be enough.
You cannot magically extend the file from the middle. Perhaps easiest to write to a new file: First copy the initial segment, then write your new data, then copy the remaining segment. When all is done, you can overwrite the original file.
According to the specification of fstream here
fstream::open
the ios::app "Sets the stream's position indicator to the end of the stream before EACH output operation." So ios::app doesn't work for replacing, seeks of any sort fail, at least for me.
Just using ios::out does wipe out the file contents preserving only the size, basically turning the file into trash.
ios::in|ios::out turned out as the only working thing for me.
Working Code: This code searches for a string (OLD-STRING) in cout.exe and replaces with a new string (NEW-STRING).
`#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
fstream ifs;
ifs.open ("C:\\Users\\user\\Desktop\\cout.exe", fstream::binary | fstream::in | fstream::out);
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
size_t pos = str.find("OLD-STRING");
if (pos != string::npos)
{
cout << "string found at position: " << int(pos) << endl;
ifs.seekp(pos);
ifs.write("NEW-STRING", 10);
}
else
{
cout << "could not find string" << endl;
}
if (ifs.is_open())
ifs.close();
return 0;
}`