0% found this document useful (0 votes)
8 views3 pages

Shortest

The document is a C++ program that allows users to create folders and files, as well as read the contents of files. It includes functions to check if a folder exists, create a new folder, create a new text file with user-defined content, and read from an existing text file. The program operates through a simple text-based menu interface.

Uploaded by

devera kristian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Shortest

The document is a C++ program that allows users to create folders and files, as well as read the contents of files. It includes functions to check if a folder exists, create a new folder, create a new text file with user-defined content, and read from an existing text file. The program operates through a simple text-based menu interface.

Uploaded by

devera kristian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#include<string>
#include<fstream>
#include<sys/stat.h>
#include<direct.h>
#include<cstdlib>

using namespace std;

bool folderExists (const string &folderPath){


struct stat info;

if(stat(folderPath.c_str(), &info)!=0){
return false;
}else if(info.st_mode & S_IFDIR){
return true;
}else{
return false;
}
}

void createFolder(){
string foldername, folderPath;

cout<<"Enter location Path: ";


getline(cin, folderPath);

if(folderExists(folderPath)){
cout<<"Folder exists !!";
}else{
if(mkdir(folderPath.c_str())==0){
cout<<"Folder created Successfully"<<endl;
}else{
cerr<<"Error creating folder"<<endl;
}
}
}

void createFile(){
string folderPath, filePath;

cout<<"Enter location Path: ";


cin>>folderPath;

if(!folderExists(folderPath)){
cout<<"Do you want to create file? ";
char choice;
cin>>choice;

if(choice=='y' || choice=='Y'){
createFolder();
}else{
cout<<"file aborted!!";
return;
}
}

string filename;
cout<<"Enter Filename: ";
cin>>filename;

filePath = folderPath + "/" + filename + ".txt";

ofstream outFile(filePath);

string content;
cout<<"Enter Content (Type END to the new line to stop)"<<endl;
while(true){
getline(cin,content);
if(content=="END")break;
outFile<<content<<endl;
}

outFile.close();

cout<<"Saved to: "<<filePath<<endl;


}

void readFile(){
string folderPath, filename, filePath;

cout<<"Enter location Path: ";


cin>>folderPath;

cout<<"Enter filename: ";


cin>>filename;

filePath = folderPath + "/" + filename + ".txt";

ifstream inFile(filePath);

string line;
cout<<"reading from "<<filename<<endl;
while(getline(inFile, line)){
cout<<line<<endl;
}
}

int main(){
int choice;

do{
cout<<"Main Menu:"<<endl;
cout<<"1. Create folder"<<endl;
cout<<"2. Create File"<<endl;
cout<<"3. Read File"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Choose option: ";
cin>>choice;

cin.ignore();

switch(choice){
case 1:
createFolder();
break;
case 2:
createFile();
break;
case 3:
readFile();
break;
case 4:
break;
default:
cout<<"Invalid Input!!"<<endl;
}

cout<<"Press enter to continue...."<<endl;


cin.get();
}while(choice!=4);
}

You might also like