I am trying to rename a file with the rename() function of stdio.h and it works but the problem is that it can only rename files located in the folder of the current project, I would like to be able to select a directory and if it is possible to change it from location in the process.
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
bool verifier;
char oldName[] = "text.txt";
char newName[] = "newText.txt";
verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamed\n";
}
else
{
std::cout << "There was a problem renaming the file\n";
}
return 0;
}
Thank you!
By default, the root directory path is the location which the executable is running in. If you want to access another folder above our outside that location, you can use an absolute path (ie C:/path/to/old.txt).
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
char oldName[] = "C:\\path\\to\\your\\proj\\text.txt"; // char oldName[] = "old.txt";
char newName[] = "C:\\test\\output\\folder\\new.txt"; // char newName[] = "newText.txt";
bool verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamed\n";
}
else
{
std::cout << "There was a problem renaming the file\n";
}
return 0;
}
Related
I am trying to just get user name. i.e., it's equivalent to whoami in ubuntu machine. But I am unable to get. I have tried following snippets.
method-1:
std::string get_username() {
struct passwd *pwd = getpwuid(getuid());
if (pwd)
return pwd->pw_name;
else
return "(?)";
}
method-2:
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
cout << "Name is : " << text;
pclose(name);
cout << endl;
return 0;
}
method-3:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << getenv("USER") << endl;
cout << getenv("HOME") << endl;
return 0;
}
The all methods are returning value as I expected. But, When I integrate this code into my Cmake project, it always returns root. I am confused why I am always getting root as response when I try with Cmake.
How to get right value instead of root?
I am trying to list all the files of a certain type in a folder, so that I can loop through them. This should be simple, surely, but I can't get it.
I have found some example using dirent.h, but I need to do this in straight c++.
What is the best way to go about this?
Thanks.
You cannot do this in "straight C++", because C++ does not have a filesystem API yet.
I'd traditionally recommend Boost.Filesystem here, but you allegedly want to "avoid using third party headers if [you] can".
So your best bet is to use POSIX dirent.h, as you have been doing all along. It's about as "non-third party" as you're going to get for the time being.
Something like this? This finds all suid files in folders you specify, but can be modified to find any number of things, or use a regex for the extension if that is what you mean by 'type'.
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <sstream>
#include <dirent.h>
#include <vector>
bool is_suid(const char *file)
{
struct stat results;
stat(file, &results);
if (results.st_mode & S_ISUID) return true;
return false;
}
void help_me(char *me) {
std::cout
<< "Usage:" << std::endl
<< " " << me << " /bin/ /usr/sbin/ /usr/bin/ /usr/bin/libexec/" << std::endl;
exit(1);
}
int main(int argc, char **argv)
{
if (argc < 2) help_me(argv[0]);
std::string file_str;
std::vector<std::string> file_list;
for (int path_num = 1; path_num != argc; path_num++) {
const char * path = argv[path_num];
DIR *the_dir;
struct dirent *this_dir;
the_dir = opendir(path);
if (the_dir != NULL) while (this_dir = readdir(the_dir)) file_list.push_back(std::string(this_dir->d_name));
std::string name;
for(int file_num = 0; file_num != file_list.size(); file_num++) {
name = file_list[file_num];
std::string path_to_file = std::string(path) + file_list[file_num];
if (is_suid(path_to_file.c_str()) == true) std::cout << path_to_file << std::endl;
}
file_list.clear();
file_list.shrink_to_fit();
}
exit(0);
}
I am new to C++.
I was trying to read a file using fstream.
here is the code,
I put the file inside the a.out directory but still cannot read it, where is my mistake?
#include<iostream>
#include<fstream>
int main()
{
std::ifstream myfile("my.txt");
int a, b;
while(myfile>>a>>b)
std::cout<<a<<b;
return 0;
}
Try:
#include <iostream>
#include <fstream>
#include <unistd.h>
int main()
{
char* name = get_current_dir_name();
std::cout << "Current Working Dir: " << name << "\n";
free(name);
std::ifstream myfile("my.txt");
if (!myfile))
{
std::cout << "Failed to open file\n";
exit(1);
}
int a, b;
while(myfile>>a>>b)
{
std::cout<<a<<b;
}
return 0;
}
Make sure that the file is located in the current directory of the .exe. This is usually the same directory as where the .exe is located on your harddrive.
If you don't know what the current directory is, I recommended you use the full path.
I have this code that opens a directory and checks if the list is not a regular file (means it's a folder) it will open it too. How can I distinguish between files and folders with C++.
here is my code if this helps :
#include <sys/stat.h>
#include <cstdlib>
#include <iostream>
#include <dirent.h>
using namespace std;
int main(int argc, char** argv) {
// Pointer to a directory
DIR *pdir = NULL;
pdir = opendir(".");
struct dirent *pent = NULL;
if(pdir == NULL){
cout<<" pdir wasn't initialized properly!";
exit(8);
}
while (pent = readdir(pdir)){ // While there is still something to read
if(pent == NULL){
cout<<" pdir wasn't initialized properly!";
exit(8);
}
cout<< pent->d_name << endl;
}
return 0;
}
One way would be:
switch (pent->d_type) {
case DT_REG:
// Regular file
break;
case DT_DIR:
// Directory
break;
default:
// Unhandled by this example
}
You can see the struct dirent documentation on the GNU C Library Manual.
For completeness, another way would be:
struct stat pent_stat;
if (stat(pent->d_name, &pent_stat)) {
perror(argv[0]);
exit(8);
}
const char *type = "special";
if (pent_stat.st_mode & _S_IFREG)
type = "regular";
if (pent_stat.st_mode & _S_IFDIR)
type = "a directory";
cout << pent->d_name << " is " << type << endl;
You'd have to patch the filename with the original directory if it differs from .
Hey everyone. I need to write a POSIX program to search through an entire file system for a specified file starting at the top directory. I've got some code which isn't done at all, but when I run it, and check to see if a particular file is a directory, it's saying this file which is not at all a directory is a directory and is trying to move into it, causing an error. I'm not sure how I can tell it that this type of file isn't a directory.
Here's my code. I know it's not perfect and I could probably do some things differently in the way of getting the directory names and passing them into the function. Either way, I'm pretty sure I have to do this recursively.
The file in question is /dev/dri/card0 and I'm running this from a Debian virtual machine.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <locale.h>
#include <langinfo.h>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
void SearchDirectory(string file_Name, string directory){
string new_Directory = directory;
DIR *dirp;
dirp = opendir(directory.c_str());
struct dirent *dptr;
struct stat statStruct;
while(dptr = readdir(dirp)){
stat(dptr->d_name, &statStruct);
if( S_ISDIR(statStruct.st_mode) ){
string check = dptr->d_name;
if ( check.compare(".") == 0 || check.compare("..") == 0 ){
continue;
}
else{
cout << dptr->d_name << " is is a directory" << endl;
new_Directory.append("/");
new_Directory.append(dptr->d_name);
SearchDirectory(file_Name, new_Directory);
}
}
else if( S_ISREG(statStruct.st_mode)){
string check = dptr->d_name;
if( check.compare(file_Name) == 0){
cout << "Found " << file_Name << " in " << directory << "/" << endl;
}
}
}
}
int main(int argc, char *argv[]){
if(argc < 2 || argc > 2){
cerr << "This program will find the specified file." << endl;
cerr << "Usage: mysearch <filename>" << endl;
return 1;
}
string file_Name = argv[1];
SearchDirectory(file_Name, "/");
return 0;
}
POSIX.2 requires a working "find" command.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>", argv[0]);
}
execlp("find", "find", "/", "-name", argv[1], "-print", (char *)NULL);
exit(EXIT_FAILURE);
}
->d_name returns just the name of the file, not the path to the file. You need to stat (not yet constructed) new_Directory instead of dptr->d_name.
You also have a problem if a directory contains more than one subdirectories. Your construction of new_Directory is incorrect for each subdirectory after the first.
You never closedir your directory handle, so you run out of resources. You should also consider loading the entire directory into an array before recursing to avoid running out of handles.
void SearchDirectory(string directory, string target_File_Name){
DIR *dirp = opendir(directory.c_str());
if (!dirp) {
perror(("opendir " + directory).c_str());
return;
}
struct dirent *dptr;
while(dptr = readdir(dirp)){
string file_Name = dptr->d_name;
string file_Path = directory + "/" + file_Name;
struct stat statStruct;
stat(file_Path.c_str(), &statStruct);
if( S_ISDIR(statStruct.st_mode) ){
if ( file_Name.compare(".") == 0 || file_Name.compare("..") == 0 ){
continue;
}
SearchDirectory(file_Path, target_File_Name);
}
else if( S_ISREG(statStruct.st_mode)){
if( file_Name.compare(target_File_Name) == 0){
cout << file_Path << endl;
}
}
}
closedir(dirp);
}
Update: Added second problem.
Update: Added third problem.
Update: Added code.
Not for the benefit of the OP, who writes "The point is to come up with a way to do it myself," but rather for the benefit of posterity, here is a way to use Boost.Filesystem:
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
// sample usage: find_file("/home", ".profile");
void find_file( const fs::path& dirPath, const std::string& fileName) {
fs::recursive_directory_iterator end;
for(fs::recursive_directory_iterator it(dirPath); it != end; ++it) {
if(it->leaf() == fileName)
std::cout << it->path() << "\n";
if(fs::is_symlink(it->symlink_status()))
it.no_push();
}
}
Use fork, execv and the Unix implemented /usr/bin/find process and redirect its output for your result area?
I'm not sure if it's POSIX or not but the nftw library function is widely available on UNIX (HP-UX, AIX, Linux).
Your problem is "search a tree for a match"
BFS and DFS are the canonical basic algorithms. Give them a start node and go.
You will get into trouble if you follow symlinks; so test for them and don't follow them.
You should be able to map each point in the *FS algorithms to a directory operation.
Since C++ is an option, why not use something like Boost.Filesystem? The Boost.Filesystem two-minute tutorial gives an example of how to implement your search using directory iterators.