How do I use std::rename with variables? - c++

In my program, I store data in different text files. The data belongs to an object, which I call rockets. For example, the rocket Saturn 5 has a text file labeled "Saturn5R.txt". I want an option to rename the rocket, and so I will need to rename the text file as well. I am using std::rename in the library. I have gotten it working with something like this:
char oldname[] = "Saturn5R.txt";
char newname[] = "Saturn6R.txt";
if (std::rename(oldname, newname) != 0) {
perror("Error renaming file");
}
This works, but I don't want to always be renaming Saturn5R.txt to Saturn6R.txt. What I want to do is to be able to rename any text file to any name, I have tried this and I get an error:
char oldname[] = rocketName + RocketNumber + "R.txt";
char newname[] = NameChoice + NumberChoice + "R.txt";
if (std::rename(oldname, newname) != 0) {
perror("Error renaming file");
}
This returns the error "[cquery] array initializer must be an initializer list or string literal".
How can I use std::rename or any other file renaming function that allows me to rename any files I want without hardcoding them in?

This has little to do with std::rename, and everything to do with how to interpolate variables into a string. A simple solution is to use std::string. It has overloaded operator + that can be used to concatenate substrings.
If you want to make the program a bit fancier, C++20 added std::format:
std::string oldname = std::format("{}{}R.txt", rocketName, RocketNumber);
std::string newname = std::format("{}{}R.txt", NameChoice, NumberChoice);
if (std::rename(oldname.c_str(), newname.c_str()) != 0) {
P.S. I recommend using std::filesystem::rename instead since it has better ways of handling errors in my opinion.

Related

I want to create a text file in cpp using ofstream

I want to create a file qbc.txt. I know how to create it, but I want to create a program that, if a file already exists with the same name, it would rename it to qbc(1).txt.
In C++17, boost's filesystem library was standardized as std::filesystem
It comes with a convenient std::filesystem::exists function.
It accepts a std::filesystem::path object, but fortunately those can be constructed with a std::string, making our program trivially easy:
std::string prefix = "qbc";
std::string extension = ".txt";
std::filesystem::path filename{prefix + extension};
int i = 0;
while (std::filesystem::exists(filename)){
filename = prefix + "(" + std::to_string(++i) + ")" + extension;
}
// now filename is "qbc(1)" or "qbc(2)" etc.
Unfortunately no compiler has full support for it at the time of this writing!
Here is a simple solution. The file_exists() function came from #Raviprakash in his response. I've added how to change the filename and try again until success. I've done an approach similar to this before in Python.
If you know that your program is the only one that will create or remove these files, then you can cache the last created one and simply create the next one instead of looping over all of the created ones every time. But this kind of optimization would only make sense if you plan to make hundreds of thousands of files this way.
#include <fstream>
#include <string>
bool file_exists(const std::string &filename) {
std::ifstream in(filename);
return in.good();
}
std::ofstream& open_new(std::ofstream &out, std::string prefix,
std::string suffix)
{
std::string filename = prefix + suffix;
unsigned int index = 0;
while (file_exists(filename)) {
index++;
filename = prefix + "(" + std::to_string(index) + ")" + suffix;
}
out.rdbuf()->open(filename, std::ios_base::out);
return out;
}
int main() {
std::string prefix = "qbc";
std::string suffix = ".txt";
std::ofstream out;
open_new(out, prefix, suffix);
out << "hello world!\n";
return 0;
}
I know the program needs some improvements but the general idea is here:
#include <fstream>
#include <string>
using namespace std;
inline bool file_exists(const std::string& name)
{
ifstream f(name.c_str());
return f.good();
}
int main()
{
string filename, name;
name = "qbc";
filename = name;
int counter = 1;
while (file_exists(filename+".txt")) {
string str = to_string(counter);
filename = name+ "(" + str + ")";
counter++;
}
filename += ".txt";
ofstream out(filename.c_str());
return 0;
}
I don't think this can be entirely solved using just the standard libraries. You can certainly keep picking a new file name until you find one that's unused and then create the new file (as the other answers have shown).
But there's an inherent race condition in that approach. What if another process creates a file between the time your program decides the name is available and the time it actually creates the file? Imagine two copies of your program both trying to write out files.
What you need is an atomic way to check for the file's existence and also to create the file. The normal way to do that is to first just try to create the file and then see if you succeeded or not. Unfortunately, I don't think the standard C++ or C libraries give you enough tools to do that. (I'd be happy to be proven wrong about that.)
Operating systems often provide APIs for doing just that. For example, Windows has GetTempFileName, which just keeps trying to create a new file until it succeeds. The key is that, once it succeeds, it keeps the file open so that it knows no other process can steal the name that's been selected.
If you tell us which OS you're using, we might be able to provide a more detailed answer.

Adding char to string in C++

I work with Eclipse and Arduino.
I want to add a char to a string. I tried to use append,insert ( but these can not be resolved)
I tried to use += but when i print the string it always have one char.Basically i deletes the string and writes only the new char i want to add in.
I tried also concat and it does the same thing.Also strcat gives me headache with the operands cause it needs a const char pointer and i want to add a char that changes.
while (theSettings.available()) {
character = theSettings.read();
if(character == '/') {
// Comment - ignore this line
while(character != '\n'){
character = theSettings.read();
}
} else if(isalnum(character)){ // Add a character to the description
Serial.println(character);
description +=character;
//description.concat(character);
Serial.println(description);
}
It sounds like what you want (for convenience) is the String object class available with the Arduino library.
http://arduino.cc/en/Reference/StringObject
If description is of the Ardunio-specific String type, you should be able to use the += operator to append.
You can do a very simple thing;
Serial.print(character);
Serial.print("");
Serial.println(description);
alternatively you can use "dtostrf" if you need to concatenate float and strings

Arduino opening SD filename as string

I am trying to open up a file that I calculate the name into a string. However, it is just giving me compile errors as shown.
for(int i=1;;i++)
{
String temp = "data";
temp.concat(i);
temp.concat(".csv");
if(!SD.exists(temp))//no matching function for call to sdclass::exists(String&)
{
datur = SD.open(temp,FILE_WRITE);
}
}
I am a java person, so I don't see why this isn't working. I tried a few string object methods but none seem to have worked. I am a bit new at arduino programming but I understand java much better. The point of this for loop is to make a new file each time the arduino reboots.
SD.open expects a character array instead of a String, you need to convert it using the toCharArray method first. Try
char filename[temp.length()+1];
temp.toCharArray(filename, sizeof(filename));
if(!SD.exists(filename)) {
...
}
Completed Code:
for(int i=1;;i++)
{
String temp = "data";
temp.concat(i);
temp.concat(".csv");
char filename[temp.length()+1];
temp.toCharArray(filename, sizeof(filename));
if(!SD.exists(filename))
{
datur = SD.open(filename,FILE_WRITE);
break;
}
}
You will find a number of functions take char arrays instead of strings.

How do I return the end of a directory

I have a function that is supposed to find the last bit of a directory in a string. eg:
"C:\Lolcats\pie\ambulance\" should return "ambulance". However it returns some strange characters ive never seen, like the male arrow-point symbol and some other weird stuff.
string App::getlastName(string cakes){
//finds the name of the last folder in a directory
string name;
string temp;//popback all of temp into name to invert it
cakes.pop_back();
char i = cakes[cakes.length()-1];
while (i != '\\'){
temp.push_back(cakes[i]);
cakes.pop_back();
i = cakes[cakes.length()-1];
} //-1?
for (int j = 0; j<temp.length(); ++j){
name.push_back(temp.back());
temp.pop_back();
}
return name;
}
This is probably one of the worst functions i've ever written, but I can't think of how else to wrangle the end off :( Can someone help me please? :D
Note that the function doesnt need to find the name of a file, it'll just be folders.
Two steps:
if it ends with a backslash character remove it:
if (!cakes.empty() && '\\' == *(cakes.end() - 1))
{
cakes.erase(cakes.end() - 1);
}
use std::string::find_last_of() to locate last backslash and std::string::substr() to extract the last part:
std::string last_part;
const size_t slash_idx = cakes.find_last_of("\\");
if (std::string::npos != slash_idx)
{
last_part = cakes.substr(slash_idx + 1);
}
If it is possible that the directory name could contain forward slashes add the additional check for the last character and just add it to the argument to find_last_of("\\/"), as it can search for more than one character.
If you remove the trailing \ off the string you can use a simple combination of rfind and substr to get the data you want.
string substring = cakes.substr(cakes.rfind("\\") + 1);
#Joel Rondeau's comment where he says that the line temp.push_back(cakes[i]) is a problem is correct, but I thought I would elaborate.
The reason is that the variable i is defined as a char not an int, but it is possible for the two to be cast implicitly. So the reason why you are getting strange characters returned is because casting a char to an int has resulted in an index value that probably does not exist in your string.
Reading your code sample it looks like you should be doing temp.push_back(cakes[cakes.length()-1]) instead (or better, store this index in a temp variable so you don't have to keep writing it every time).
Your method depends on the string already being in the correct format with no assertions or error checking--not a good idea.
I would just do something like:
char* directoryName = strrchr(fullPath, '\\') + 1;
after you have trimmed off the trailing '\'.

Changing last 5 char of array

I have a program that encrypts files, but adds the extension ".safe" to the end. So the end result is something like "file.txt.safe"
When I go to decrypt the file, the user enters the file name again: "file.txt.safe" which is saved to a char. Now I want to remove ".safe" and rename the file to its original name.
I have tried the following, but nothing seems to happen and there are no errors.
Decrypt (myFile); //decrypts myFile
char * tmp = myFile;
char * newFile;
newFile = strstr (tmp,".safe"); //search tmp for ".safe"
strncpy (newFile,"",5); //replace .safe with ""
rename (myFile, newFile);
I'm sure I'm missing something obvious, but if this approach doesn't work, I'm looking for any simple method.
Edited to add:
(copied by moderator from poster's response to K-ballo)
Thanks everyone. I took the std::string approach and found this to work:
Decrypt(myFile);
string str = myFile;
size_t pos = str.find(".safe");
str.replace(pos,5,"");
rename(myFile, str.c_str());
For what you want to do, simply changing the strncpy line to this will work:
*newFile = '\0';
This would still have problems if the filename contains an early .safe (like in file.safest.txt.safe), or if it does not contain the substring .safe at all. You would be better of searching from the end of the array, and making sure you do find something.
This seems like a better approach (although in C++ it would be better to just go with std::string):
char* filename = ...;
size_t filename_length = strlen( filename );
int safe_ext_pos = filename_length - 5; // 5 == length of ".safe"
if( safe_ext_pos > 0 && strcmp( ".safe", filename + safe_ext_pos ) == 0 )
filename[ safe_ext_pos ] = '\0';
This is the std::string version of the code:
std::string filename = ...;
int safe_ext_pos = filename.length() - 5; // 5 == length of ".safe"
if( safe_ext_pos > 0 && filename.compare( safe_ext_pos, 5, ".safe" ) == 0 )
filename.erase( safe_ext_pos );
You should take care:
my.safe.file.txt.safe
Instead of just searching for '.safe' and removing it or truncating the filename at the first one, you should ensure that it's actually at the end of the string:
std::string myfile = ...
Decrypt(myFile);
const std::string extension_to_remove = ".safe";
if (decryption is successful &&
myfile.size() >= extension_to_remove.size() &&
myfile.substr(myfile.size()-5) == extension_to_remove)
{
std::string newFile = myfile.substr(0, myfile.size()-5);
rename(myFile, newFile);
}
Also a note on filename extensions. It's really a pretty awful practice for software to identify file types using a special format in the filename.* It's fine for humans to organize their files with special naming conventions, but software should by and large be oblivious to it, except perhaps to make it easy for humans to use the conventions they want.
So your code for decrypting a file shouldn't be doing this task. Instead your decryption code should take a file to decrypt and a file to contain the output. Then your code for computing the output filename from the encrypted file's name should exist somewhere else, such as in the user interface where the user tells you the output filename. Your code would remove '.safe' if it exists and supply the modified name as the default output filename, to be confirmed by the user.
void perform_decryption(std::string const &encrypted, std::string const &decrypted) {
Decrypt(encrypted);
if (decryption is successful && encrypted!=decrypted)
rename(encrypted, decrypted);
}
std::string default_decrypted_name(std::string const &filename) {
const std::string extension_to_remove = ".safe";
if (filename.size() >= extension_to_remove.size() &&
filename.substr(filename.size()-extension_to_remove.size()) == extension_to_remove)
{
return filename.substr(0, filename.size()-extension_to_remove.size());
}
return filename + ".decrypted";
}
* here are some reasons against filename extensions:
filename extensions are not unique, in some circumstances causing conflicts where a file's type cannot be positively identified. (the fact that they can't even perform their intended purpose really ought to be enough...)
It degrades the usability of the filename for organizing. When 'myfile.txt' is renamed to 'myfile.txt.old' it's no longer seen as a text file.
It's caused security issues because fake type metadata can be mistaken for real type metadata when the real type metadata is hidden.
and more...