Skip to content
Sign upLog in
← Back to Community

How to check if a file exists

Profile icon
JacobMcPherson1

How am I able to check if a file already exists in the repl

Voters
Profile icon
Jimix-Dzhimiks
Profile icon
JohnnySantamari
Profile icon
SumitBhutta
Profile icon
KelvinFernandez
Profile icon
ddaenishh
Profile icon
JacobMcPherson1
Comments
hotnewtop
Profile icon
rabiaataleb26

in cant even create .replit file it says a file or a folder with that name is already exists how to fix it?

Profile icon
IshantKumar3
Profile icon
btfuss

Use the system() function is the c++ access to the bash commands. Then refer to this article: https://linuxize.com/post/bash-check-if-file-exists/

Good luck!

Profile icon
JacobMcPherson1

@2plus2is4hoi
I found out a way to do it right before you commented. Sorry lol

Profile icon
31heitorgarcia0

@JacobMcPherson1
can you please tell me it?

Profile icon
JacobMcPherson1

@31heitorgarcia0

#include <iostream> #include <fstream> #include <string.h> using namespace std; int main() { char filename[50]; cout << "Enter file-name: \n"; cin >> filename; ofstream textpad(filename); ifstream ifile; ifile.open(filename); if (ifile) { // your code here } }