I am writing a simple Mail Server-Client App which is taking a users input for a receiver, a subject line and a message. To store the message, there is a directory in which a subdirectory is being created for every user that receives a message and in the folder each message is being stored with the message index + subject line. e.g. User Bob receives a message with the subject "Hello World", inside the mailspoooler directory a subdirectory with Bob's name is being created and inside the subdirectory a file called '1. Hello World'. The problem with that is, that a user can now enter a path into the subject line and store the information of his message somewhere else, where it's not intended. Is there a way to ecsape the input if someone were to enter a path? Down below you have the code of the function that creates the files. The vector msg is each line of the user input saved and msg[1] is always the subject line
void send(fs::path mailspooler, char* buffer, std::vector<std::string> msg, fs::path current, std::string user){
//switching to mailspooler directory//
try{
fs::current_path(mailspooler);
}
catch(...){
std::cerr << "An error occured with the filesystem" << std::endl;
strcat(buffer, "ERR");
}
//creates subfolder in directory with name of receiver//
fs::create_directory(msg.at(0));
//changing to users directory
try{
fs::current_path(mailspooler.string() + "/" + user);
}
catch(...){
std::cerr << "An error occured with the filesystem" << std::endl;
}
int index = std::distance(fs::directory_iterator(fs::current_path()), {}); // checks to see how many files are already in directory
//create file
std::ofstream user_msg(std::to_string(index + 1) + ". " + msg.at(1));
user_msg << "Sender: " << user << std::endl << "Subject: " << msg.at(1) << std::endl << "Message: " << std::endl;
for(unsigned int i = 2; i<msg.size(); i++)
user_msg << msg.at(i) << std::endl; //writing every single line from the message into file
user_msg.close();
//changing back to base directory
try{
fs::current_path(current);
strcat(buffer, "OK");
}
catch(...){
std::cerr << "An error has occured with the filesystem" << std::endl;
strcat(buffer, "ERR");
}
}
Related
I am trying to stream the data in the retrieved s3 file into a local file on disk. However, when I try my current code, I do not get a file stored on my computer, or in any location near the code.
I first request the object from s3 using a getObjectOutcome. After success, I want to create an ofstream and redirect the objects stream buffer to my local object so that I can create a file on disc. However, the following code does not create a file on my computer. What am I doing wrong?
Here is the get object function:
bool GetObject(const Aws::String& objectKey,
const Aws::String& fromBucket,
const Aws::Client::ClientConfiguration& clientConfig) {
Aws::S3::S3Client client(clientConfig);
Aws::S3::Model::GetObjectRequest request;
request.SetBucket(fromBucket);
request.SetKey(objectKey);
Aws::S3::Model::GetObjectOutcome outcome =
client.GetObject(request);
if (!outcome.IsSuccess()) {
const Aws::S3::S3Error& err = outcome.GetError();
std::cerr << "Error: GetObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
}
else {
std::cout << "Successfully retrieved '" << objectKey << "' from '"
<< fromBucket << "'." << std::endl;
std::ofstream localfile;
localfile.open(objectKey.c_str(), std::ios::out | std::ios::binary);
auto retrieved = outcome.GetResult().GetBody().rdbuf();
localfile << retrieved;
std::cout << "Done!";
}
return outcome.IsSuccess();
}
Here is an image of the memory for local file and retrieved:
Would someone teach me what I am doing this wrong, or how to correctly download data from s3 to disc?
Thanks.
I tried downloading some data from s3 to disc. I am having trouble outputting this data via stream buffer to local file. I have been looking online and cannot find a similar problem.
Update:
I am now on my second day of trying to figure this out to no avail. For some reason, the code will not even output a file after it has been created to the directory I have set up for the .nc files to be written to.
I have tried the following updates:
bool GetObject(const Aws::String& objectKey,
const Aws::String& fromBucket,
const Aws::Client::ClientConfiguration& clientConfig) {
Aws::S3::S3Client client(clientConfig);
Aws::S3::Model::GetObjectRequest request;
request.SetBucket(fromBucket);
request.SetKey(objectKey);
Aws::S3::Model::GetObjectOutcome outcome =
client.GetObject(request);
if (!outcome.IsSuccess()) {
const Aws::S3::S3Error& err = outcome.GetError();
std::cerr << "Error: GetObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
}
else {
std::cout << "Successfully retrieved '" << objectKey << "' from '"
<< fromBucket << "'." << std::endl;
//create the filename, which will be the objectKey
std::string local_file_name = "./netcdf/" + objectKey;
std::ofstream local_file(local_file_name, std::ios::binary);
auto &retrieved = outcome.GetResult().GetBody();
local_file << retrieved.rdbuf();
std::cout << "Done!";
}
return outcome.IsSuccess();
}
Then, after opening the ./netcdf folder, there is no output.
Here is the file structure inside my project for reference with the code:
I am still confused as to what I need to do here.
Thank you for all of the help you can offer!
You are using a folder with "./" at the front. This means that the file will be relative to the current working directory (cwd) of the binary. That is likely not the src folder
Just to get past your problem, use a full absolute path to see if the rest of your code works.
Also, try adding
// You need "#include <filesystem>" for the next line
cout << std::filesystem::current_path();
To see where the files you made might be
I'm implementing a simple program using text files. Users can register/login accounts that are saved to a txt file.
One of the options is to post a message to a "Post board" that will store the user's name followed by all their posts.
For example:
Leo: Hello
Tony: Bye
I'm able to successfully read the text file and output the message to the console but the username is not displaying, only the message.
For refrence I have a postMessage() function that uses getline() to concatante the username using
ofstream << getUsername() + ": " + string::message.
I have quite a bit of code so I cannot share all of it, however, here are the pieces in question.
public:
char postMessage(User& currentUser, bool loggedIn) { //Will post a message to the board. Passes a User object.
if (loggedIn == false) {
std::cout << "You must be logged in to post!" << std::endl;
userLoginRegisterPrompt();
return 0;
}
char messageState; //Code to verify if message was posted.
std::string message; //Actual message string
std::cout << "Enter your text: " << std::endl;
std::cin.ignore(); //Clears string buffer.
getline(std::cin,message);
std::ofstream post("C:/Users/19097/Desktop/Programs/Registration/Registration/UserPosts/-Posts.txt", std::ios::app); //File with user posts.
post << currentUser.getUserName() << ": " << message << std::endl;
logAction(currentUser.getUserID(), "Made a post");
}
Here is the loadPosts() method to read from the text file and display it to console.
static bool loadPosts() { //Will be for viewing the post history.
std::ifstream post("C:/Users/19097/Desktop/Programs/Registration/Registration/UserPosts/-Posts.txt");
std::string line;
if (!post) {
std::cout << "No board exists!" << std::endl;
return false;
}
while (post >> line) {
getline(post, line);
std::cout << line << std::endl;
}
}
This is what some of the posts read like in the actual txt file.
This is what shows on the console when using loadPosts()
IN IOS app, module written in C++ I am writing my data (map of basic strings and integers) to a text file. Using following method:
bool Recognizer::saveMap(const char * s)
{
if(trainingData.model && !trainingData.model.empty()) {
const string filename = string(s);
std::ofstream file(s, ios_base::trunc );
try{
if(! file.is_open())
{
file.open(s);
}
for (map<String,int>::iterator it=trainingData.idMap.begin(); it!=trainingData.idMap.end(); ++it)
{
cout << it->second << " " << it->first << endl;
file << it->first << endl << it->second << endl;
}
file.close();
}
catch(cv::Exception & e){
if(file.is_open())
file.close();
int code = e.code;
string message = e.err;
cerr << "cv::Exeption code: " << code << " " << message << endl;
return false;
}
std::streampos fileLength = iosFileSize(s);
cout << "Saved map to: " << filename << " length: " << fileLength << endl;
return true;
}
return false;
}
My contains one entry and console output indicates that two lines: string, string representing number have been written to my file.
Subsequent opening file for reading and reading using getline or using stream operator indicates that file is empty:
bool Recognizer::loadMap(const char * s)
{
std::streampos fileLenght = iosFileSize(s);
std::ifstream file(s, ios::in);
try{
if(file.is_open())
{
string name;
string lineName;
string lineTag;
int tag;
int count = 0;
while(getline(file,name))
{
if(getline(file,lineTag))
{
tag = stoi(lineTag,0,10);
count++;
cout << tag << " " << name << endl;
trainingData.idMap[name]=tag;
trainingData.namesMap[tag]=name;
}
}trainingData.personsCount=count;
file.close();
}
}
catch(cv::Exception & e){
if(file.is_open())
file.close();
int code = e.code;
string message = e.err;
cerr << "cv::Exeption code: " << code << " " << message << endl;
return false;
}
cout << "Loaded map from: " << s << " lenght: "<< fileLenght << endl;
return true;
}
I also copied from one of stackoverflow answers method returning file lenght and using it to verify lenghth of the file after write operation:
std::streampos iosFileSize( const char* filePath ){
std::streampos fsize = 0;
std::ifstream file( filePath, std::ios::binary );
fsize = file.tellg();
file.seekg( 0, std::ios::end );
fsize = file.tellg() - fsize;
file.close();
return fsize;
}
The file path passed to saveMap and loadMap seems to be legit. With path that the app could not write to, attempt to write caused exception.
There are no errors returned by write operation but both, attempts to read and iosFileSize() indicate that file is empty.
I am not sure if i need call file.open() and file.close() or file is open and closed automatically when output stream is created and later goes out of scope.
I experimented with those with the same result ( call to file.is_open returns true so the block calling file.open() is skipped.
What am I doing wrong?
I appreciate all responses.
It does not seem like you call file.flush(); anywhere in Recognizer::saveMap() after writing to the file stream. std::ofstream::flush() saves changes you've made to the file. Add file.flush(); between when you make changes to the code and when you close the file. See if that remedies your issue.
I also had the same issue. Using file.flush() everytime after you insert to a file can save your file.
However if you insert something like this, say,
file << "Insert This"; You will need to add file.flush().
But some people have issues, like if you just insert file << "Insert This" << endl; , this works fine. The key point here is that, std::endl calls flush() everytime it is used internally. you can say it is a shortend form of "\n" + flush().
I believe from looking at your code that you are overwriting your data when you open the file in the second program you should be using something like this.
std::fstream fs;
fs.open ("test.txt", ios::app)
instead of doing the ios::in
I have a C++ program that reads a config file and gets the directories.
What I want to do now is to execute an .exe program using the directory settings from the config file.
Here is a piece of my code:
int main(){
ConfigFile cfg("htbaseconfig.properties");
bool exists = cfg.keyExists("backuplocation");
exists = cfg.keyExists("logdir");
exists = cfg.keyExists("execdir");
exists = cfg.keyExists("fulldir");
exists = cfg.keyExists("incdir");
exists = cfg.keyExists("appdir");
std::string bkploc = cfg.getValueOfKey<std::string>("backuplocation");
std::cout << "Backup Location: " << bkploc << "\n";
std::string bkplogdir = cfg.getValueOfKey<std::string>("logdir");
std::cout << "Log Location: " << bkplogdir << "\n";
std::string bkpexec = cfg.getValueOfKey<std::string>("execdir");
std::cout << "Exec Directory: " << bkpexec << "\n";
std::string bkpfulldir = cfg.getValueOfKey<std::string>("fulldir");
std::cout << "Full Directory: " << bkpfulldir << "\n";
std::string bkpappdir = cfg.getValueOfKey<std::string>("appdir");
std::cout << "Real app Directory: " << bkpappdir << "\n\n\n";
for( ; ; ) {
Sleep(6000);
ShellExecute(NULL, L"open", , L"C:\\teste.htm", NULL,SW_SHOWNORMAL);
}
std::cin.get();
return (0);}
Inside the ShellExecute, I wanted to execute the following line parsing the config options:
$execdir/program.exe $logdir/log.txt $bkpappdir $bkploc
How do I do this? I want to execute my program with the variables I get on std::cout.
You must pass to ShellExecute, instead of the second NULL, a string (c string, a char[]) that contains all parameters, like if you are passing them to the command line.
So Will be something like
ShellExecute(NULL, L"open", , L"C:\\teste.htm", "option=param option2=param2",SW_SHOWNORMAL);
Depends on how you parse them (or how they are parsed) from the other exe file
I am having some trouble with sprintf and fstream functions in order to create new text files for a POS program/check whether the file already exists. I don't know if i am doing something wrong because the same set of functions works fine in other places in my code...
This particular section of code is taking input from the user to create a details file, the name is made up of the first and last name details that were entered into the system. For some reason the new file is not being created. When I step through the program I can see that the custDetC variable is being filled with the correct data. I have also included the file existence check as it may or may not have something to do with the issue at hand...
Tony Mickel
sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
cout << custDetC << endl;
FileEX = FileExists(custDetC);
if (FileEX == true)
{
fopen_s(&custDetF,custDetC, "rt");
fprintf(custDetF, "%s %s\n", firstName, lastName);
fprintf(custDetF, "$d\n", phoneNo);
fprintf(custDetF, "%s $s\n", unitHouseNum, street);
fprintf(custDetF, "%s %s %d", suburb, state, postCode);
fclose(custDetF);
}
else
{
char *buf = new char[100];
GetCurrentPath(buf);
cout << "file " << custDetC << " does not exist in " << buf << endl;
}
}
bool FileExists(char* strFilename)
{
bool flag = false;
std::fstream fin;
// _MAX_PATH is the maximum length allowed for a path
char CurrentPath[_MAX_PATH];
// use the function to get the path
GetCurrentPath(CurrentPath);
fin.open(strFilename, ios::in);
if( fin.is_open() )
{
//cout << "file exists in " << CurrentPath << endl;
flag = true;
}
else
{
//cout << "file does not exist in " << CurrentPath << endl;
flag = false;
}
fin.close();
return flag;
}
You seem to be opening the file for reading, but you need to open it for writing.
Instead of "rt" use "wt" in fopen_s()