Related
I have a C program that finds duplicate files within a directory. The program is executed on the command line and passed 2 arguments. One is the parent directory, and argument two is the file name. It is working code in c, but I have a GUI and other files for "microservices" written in c++.
How would one call this C code from a c++ file?
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
//Compile: gcc dreamduplicatefinder.c -o dreamduplicatefinder.exe
//Run: ./dreamduplicateFinder.exe parent_dir filename...
#define false 0
#define true 1
int duplicateCount = 0;
int FindDuplicates(char* path, char* fileName);
int CompareFiles(char* originalFile, char* currFile);
int main(int argc, char *argv[])
{
//Two additional arguments are expected: Parent dir, file to find duplicates of...
if (argc != 3)
{
printf("Usage: %s 'Base Directory' 'File Name'\n", argv[0]);
return -1;
}
//argv[1] = base dir, argv[2] = file to find duplicates of; e.g argv[1] = /home,
//argv[2] = "file.txt"...
FindDuplicates(argv[1], argv[2]);
printf("\n\nFound %d duplicate(s)\n", duplicateCount);
return 0;
}
int FindDuplicates(char* path, char* fileName)
{
DIR *dir;
struct dirent *dp;
struct dirent *result;
struct stat statp;
char absoluteFilePath[255];
if ((dir = opendir(path)) == NULL)
{
//printf(dir); //error could becuase trying to open shortcut or corrupt folder.
printf("%s\n",path);
perror("Failed to open directory");
return -1;
}
while ((dp = readdir(dir)) != NULL)
{
//readdir returns . and .. which we should ignore...
if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
{
//find file full path, relative to base path. e.g, a /home/file.txt...
//copy path to absoluteFilePath...
strcpy(absoluteFilePath, path);
//append / at end...
strcat(absoluteFilePath, "/");
//append filename to path...
strcat(absoluteFilePath, dp->d_name);
//check if the current file is actually file or dir...
stat(absoluteFilePath, &statp);
if (S_ISDIR(statp.st_mode)) //is a directory...
{
//recurse through this dir...
FindDuplicates(absoluteFilePath, fileName);
}
else if (S_ISREG(statp.st_mode)) //is a file...
{
//check for duplicates here...
//compare current file with the file specified by user...
if (strcmp(fileName, absoluteFilePath))
{
if (CompareFiles(fileName, absoluteFilePath))
{
//yes, duplicate; print it...
printf("%s\n", absoluteFilePath);
duplicateCount++;
}
}
} //end else if (S_ISREG(statp.st_mode))...
} //if (strcmp(dp->d_name, ".") && strcmp(dp->d_name,".."))...
} //end while...
closedir(dir);
return 0;
}
int CompareFiles(char* originalFile, char* currFile)
{
//two step comparison: (1) first check size; if not same, return false.
//If equal, (2) compare file content.If equal, return true, false otherwise...
struct stat statOriginal, statCurr;
stat(originalFile, &statOriginal);
stat(currFile, &statCurr);
//Step 1...
if ((int)statOriginal.st_size != (int)statCurr.st_size) //size not same...
return false;
//Step 2...
//size matches, files can be same; confirm it by matching both file contents...
int fdOriginal = open(originalFile, O_RDONLY);
int fdCurr = open(currFile, O_RDONLY);
if (fdOriginal == -1 || fdCurr == -1)
return false; //error occurred, not sure if file is duplicate...
//we will read file in small chunks and compare...
int chunkSize = 1024, bytesRead;
char *bufferOriginal = (char*)malloc(chunkSize * sizeof(char));
char *bufferCurr = (char*)malloc(chunkSize * sizeof(char));
while (true)
{
//read file in chunk...
bytesRead = read(fdOriginal, bufferOriginal, chunkSize);
if (bytesRead <= 0)
break; //end of file...
bytesRead = read(fdCurr, bufferCurr, bytesRead);
//compare buffer...
if (strcmp(bufferOriginal, bufferCurr)) //if content not matching...
return false;
}
return true;
}
My errors include: (from compareFiles function)
2x 'open' identifier not found
2x 'read' identifier not found
The working code for those curious.
Thank you #MarcusMüller & #JesperJuhl
#include "stdafx.h" //there is nothing in this header
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Compile: gcc <name of this file>.cpp -o <nameOfThisFile>.exe
//Run: <nameOfThisFile> parent_dir filename...
#define false 0
#define true 1
int duplicateCount = 0;
int FindDuplicates(char* path, char* fileName);
int CompareFiles(char* originalFile, char* currFile);
int main(int argc, char *argv[])
{
//Two additional arguments are expected: Parent dir, file to find duplicates of...
if (argc != 3)
{
printf("Usage: %s 'Base Directory' 'File Name'\n", argv[0]);
return -1;
}
//argv[1] = base dir, argv[2] = file to find duplicates of; e.g argv[1] = /home,
//argv[2] = "file.txt"...
FindDuplicates(argv[1], argv[2]);
printf("\n\nFound %d duplicate(s)\n", duplicateCount);
return 0;
}
int FindDuplicates(char* path, char* fileName)
{
DIR *dir;
struct dirent *dp;
struct dirent *result;
struct stat statp;
char absoluteFilePath[255];
if ((dir = opendir(path)) == NULL)
{
//possibly trying to open shortcut or corrupt folder typically.
printf("Failed to open directory %s \n",path);
return -1;
}
while ((dp = readdir(dir)) != NULL)
{
//readdir returns . and .. which we should ignore...
if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
{
//find file full path, relative to base path. e.g, a /home/file.txt...
//copy path to absoluteFilePath...
strcpy(absoluteFilePath, path);
//append / at end...
strcat(absoluteFilePath, "/");
//append filename to path...
strcat(absoluteFilePath, dp->d_name);
//check if the current file is actually file or dir...
stat(absoluteFilePath, &statp);
if (S_ISDIR(statp.st_mode)) //is a directory...
{
//recurse through this dir...
FindDuplicates(absoluteFilePath, fileName);
}
else if (S_ISREG(statp.st_mode)) //is a file...
{
//check for duplicates here...
//compare current file with the file specified by user...
if (strcmp(fileName, absoluteFilePath))
{
if (CompareFiles(fileName, absoluteFilePath))
{
//yes, duplicate; print it...
printf("This is a duplicate! %s\n", absoluteFilePath);
duplicateCount++;
}
}
} //end else if (S_ISREG(statp.st_mode))...
} //if (strcmp(dp->d_name, ".") && strcmp(dp->d_name,".."))...
} //end while...
closedir(dir);
return 0;
}
int CompareFiles(char* originalFile, char* currFile)
{
//two step comparison: (1) first check size; if not same, return false.
//If equal, (2) compare file content.If equal, return true, false otherwise...
struct stat statOriginal, statCurr;
stat(originalFile, &statOriginal);
stat(currFile, &statCurr);
//Step 1...
if ((int)statOriginal.st_size != (int)statCurr.st_size) //size not same...
return false;
FILE* fdOriginal;
if (fdOriginal = fopen(originalFile, "r")) {
if (fdOriginal == NULL) { fputs("File error", stderr); return false; }
}
else return false; //error occurred, not sure if duplicate
FILE* fdCurr;
if (fdCurr = fopen(currFile, "r")) {
if (fdCurr == NULL) { fputs("File error", stderr); return false; }
}
else return false;
int chunkSize = 1024, objsRead;
char *bufferOriginal = (char*)malloc(chunkSize * sizeof(char));
if (bufferOriginal == NULL) { fputs("Memory error for buff orig", stderr); exit(2); }
char *bufferCurr = (char*)malloc(chunkSize * sizeof(char));
if (bufferCurr == NULL) { fputs("Memory error for buff curr", stderr); exit(2); }
while (true)
{
//read file in chunk...
//std::size_t fread( void* buffer, std::size_t size, std::size_t count, std::FILE* stream );
objsRead = fread(bufferOriginal, sizeof(char), chunkSize , fdOriginal);
if (objsRead <= 0)
break; //end of file...
objsRead = fread(bufferCurr, sizeof(char), objsRead, fdCurr);
//compare buffer...
if (strcmp(bufferOriginal, bufferCurr)) //if content not matching...
return false;
}
return true;
}
You usually just wouldn't do that. You'd wrap it in a C function, and compile it to an object file.
Then you'd include your C header with extern "C" {…}, and just call that function from C++.
When building your executable, you'd link in the object file containing your C function. Done!
Note: C isn't C++, and albeit your code not being illegal in C++ (as far as I can instantly tell), it does very "ugly" things (like #defineing true and false – ugh, that would already be a bad idea in C, to be honest). So, deal with it like you would deal with code in Fortran, or Java, or any other language that has a calling convention that you can use from C++ (which, usually, is the C calling convention): Just use it as an extern object.
Using ::open and ::read should cause the functions to be found.
You may also want to replace the C headers (like "string.h") with their C++ equivalent versions (like "cstring").
Your defines for true and false should also go. In C++ those are proper bools, not integers. This means the return type of CompareFiles should be changed to bool.
And you should wrap duplicateCount in an anonymous namespace - or return it from the function that updates it (either by returning a small struct with two ints, or by using a std::pair or std::tuple) - global variables are evil.
How do I copy a file from one folder to another folder using C++?
This should be the minimal code required:
#include <fstream>
// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
std::ifstream src(SRC, std::ios::binary);
std::ofstream dest(DEST, std::ios::binary);
dest << src.rdbuf();
return src && dest;
}
int main(int argc, char *argv[])
{
return copyFile(argv[1], argv[2]) ? 0 : 1;
}
it glosses around some potentially complicated issues: error handling, filename character encodings... but could give you a start.
With std::filesystem::copy_file from C++17:
#include <exception>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path sourceFile = "path/to/sourceFile.ext";
fs::path targetParent = "path/to/target";
auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
try // If you want to avoid exception handling, then use the error code overload of the following functions.
{
fs::create_directories(targetParent); // Recursively create target directory if not existing.
fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
}
catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
{
std::cout << e.what();
}
}
I've used std::filesystem::path::filename to retrieve the source filename without having to type it manually. However, with std::filesystem::copy you can omit passing the filename to the target path at all:
fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
Change the behaviour of both functions with std::filesystem::copy_options.
If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().
Here's a previous question covering copy_file():
How to use copy_file in boost::filesystem?
This is how you can do this.
include c++ library <windows.h>
Use function
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
All Done :)
The code below will copy all the file from one directory to another.
Its working code in C++
#include <windows.h>
/*
BOOL Copy(char r_szPath[1024], char r_szDir[1024])
{
char l_szTemp[2048] = {0};
sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
if(IsDirectory(
}*/
#include <stdio.h>
#include<conio.h>
BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[1025] = {0};
memcpy(l_szTmp,r_szSrcPath,1024);
char l_szSrcPath[1025] = {0};
char l_szDesPath[1025] = {0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
char l_szNewSrcPath[1025] = {0};
char l_szNewDesPath[1025] = {0};
strcat(l_szTmp,"*");
hFind = FindFirstFile(l_szTmp, &FindFileData);
if(hFind == NULL) return FALSE;
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName,"."))
{
if(strcmp(FindFileData.cFileName,".."))
{
printf ("The Directory found is %s<BR>, FindFileData.cFileName);
sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__Copy(l_szNewSrcPath,l_szNewDesPath);
}
}
}
else
{
printf ("The File found is %s<BR>, FindFileData.cFileName);
char l_szSrcFile[1025] = {0};
char l_szDesFile[1025] = {0};
sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
}
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return TRUE;
}
int main(int argc, char *argv[])
{
__Copy("C:\fcdb\","E:\sandy\");
getch();
return 0;
}
So my goal is to create a console app using visual C++ lang which opens a file ( in my case it is a MIME file) and finds a certain string. In my case it sounds - Content-Disposition: attachment; filename="file.smth".
And then the app shows the file.smth
So here is what I have done. It has some problems that I am not able to find.When I run a console app It gets stuck at finding a filename.
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
bool ShowFile(char * FileName, char * Name)
{
FILE* file;
if (fopen_s(&file, FileName, "rt") != 0) { return false; }
while (!feof(file))
{
char AttName[100];
int a = sscanf_s("Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
Name = AttName;
}
fclose(file);
return true;
}
int main(int argc, char* argv[])
{
char FileName[100];
if (argc == 2) strcpy_s(FileName, _countof(FileName), argv[1]);
else {
printf("Source file name: "); gets_s(FileName, _countof(FileName));
}
char Name[100];
ShowFile(FileName, Name);
printf("%s \n", Name);
system("pause");
return 0;
}
Thank you for your attention!
The function ShowFile can be improved.
Suggestion 1
If you would like the function to return the name of the file (file.smth) by the second argument, change it to std::string&.
//bool ShowFile(char * FileName, char * Name)
bool ShowFile(char * FileName, std::string& Name)
As the function stands right now, you are changing Name in the function to point to a local variable. But that has no impact on the calling function. The line
Name = AttName;
is completely useless.
Suggestion 2
You haven't added any code to read the data from the file. The line
int a = sscanf_s("Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
has the format specification as the first argument, not the string from which to read the data. It is missing the source string.
You need to add code to read lines of text and try to extract AttName from those lines.
Suggestion 3
Don't use while (!feof(file)). See Why is “while ( !feof (file) )” always wrong?.
You need something like:
char line[200];
while ( fgets(line, sizeof(line), file) )
{
char AttName[100];
int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
...
}
Suggestion 4
Always check the returned value of scanf family of functions to make sure that the function was able to assign data to the variables. Don't assume it succeeded.
int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
// Name = AttName;
if ( a == 1 )
{
Name = AttName;
}
Suggestion 5
Add a flag to indicate that Name was successfully read.
Revised function
bool ShowFile(char * FileName, std::string& Name)
{
bool status = false;
FILE* file;
if (fopen_s(&file, FileName, "rt") != 0) { return false; }
char line[200];
while (fgets(line, sizeof(line), file))
{
char AttName[100];
int a = sscanf_s(line, "Content-Disposition: attachment; filename=\"%[^\"]\"", AttName,_countof(AttName));
if ( a == 1 )
{
Name = AttName;
// Got what we are looking for.
// Set the status and break out of the loop.
// There is no need to look for Name any more.
status = true;
break;
}
}
fclose(file);
return status;
}
How do I copy a file from one folder to another folder using C++?
This should be the minimal code required:
#include <fstream>
// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
std::ifstream src(SRC, std::ios::binary);
std::ofstream dest(DEST, std::ios::binary);
dest << src.rdbuf();
return src && dest;
}
int main(int argc, char *argv[])
{
return copyFile(argv[1], argv[2]) ? 0 : 1;
}
it glosses around some potentially complicated issues: error handling, filename character encodings... but could give you a start.
With std::filesystem::copy_file from C++17:
#include <exception>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path sourceFile = "path/to/sourceFile.ext";
fs::path targetParent = "path/to/target";
auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
try // If you want to avoid exception handling, then use the error code overload of the following functions.
{
fs::create_directories(targetParent); // Recursively create target directory if not existing.
fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
}
catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
{
std::cout << e.what();
}
}
I've used std::filesystem::path::filename to retrieve the source filename without having to type it manually. However, with std::filesystem::copy you can omit passing the filename to the target path at all:
fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
Change the behaviour of both functions with std::filesystem::copy_options.
If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().
Here's a previous question covering copy_file():
How to use copy_file in boost::filesystem?
This is how you can do this.
include c++ library <windows.h>
Use function
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
All Done :)
The code below will copy all the file from one directory to another.
Its working code in C++
#include <windows.h>
/*
BOOL Copy(char r_szPath[1024], char r_szDir[1024])
{
char l_szTemp[2048] = {0};
sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
if(IsDirectory(
}*/
#include <stdio.h>
#include<conio.h>
BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[1025] = {0};
memcpy(l_szTmp,r_szSrcPath,1024);
char l_szSrcPath[1025] = {0};
char l_szDesPath[1025] = {0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
char l_szNewSrcPath[1025] = {0};
char l_szNewDesPath[1025] = {0};
strcat(l_szTmp,"*");
hFind = FindFirstFile(l_szTmp, &FindFileData);
if(hFind == NULL) return FALSE;
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName,"."))
{
if(strcmp(FindFileData.cFileName,".."))
{
printf ("The Directory found is %s<BR>, FindFileData.cFileName);
sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__Copy(l_szNewSrcPath,l_szNewDesPath);
}
}
}
else
{
printf ("The File found is %s<BR>, FindFileData.cFileName);
char l_szSrcFile[1025] = {0};
char l_szDesFile[1025] = {0};
sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
}
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return TRUE;
}
int main(int argc, char *argv[])
{
__Copy("C:\fcdb\","E:\sandy\");
getch();
return 0;
}
I am executing a system() function which returns me a file name. Now I dont want to display the output on the screen(ie the filename) or pipe to a newfile. I just want to store it in a variable. is that possible? if so, how?
thanks
A single filename? Yes. That is certainly possible, but not using system().
Use popen(). This is available in c and c++, you've tagged your question with both but are probably going to code in one or the other.
Here's an example in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fpipe;
char *command = "ls";
char c = 0;
if (0 == (fpipe = (FILE*)popen(command, "r")))
{
perror("popen() failed.");
exit(EXIT_FAILURE);
}
while (fread(&c, sizeof c, 1, fpipe))
{
printf("%c", c);
}
pclose(fpipe);
return EXIT_SUCCESS;
}
Well,There is one more easy way by which you can store command output in a file which is called redirection method. I think redirection is quite easy and It will be useful in your case.
so For Example this is my code in c++
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
system("ls -l >> a.text");
return 0;
}
Here redirection sign easily redirect all output of that command into a.text file.
You can use popen(3) and read from that file.
FILE *popen(const char *command, const char *type);
So basically you run your command and then read from the FILE returned. popen(3) works just like system (invokes the shell) so you should be able to run anything with it.
Here is my C++ implementation, which redirects system() stdout to a logging system. It uses GNU libc's getline(). It will throw an exception if it can't run the command, but will not throw if the command runs with non-zero status.
void infoLogger(const std::string& line); // DIY logger.
int LoggedSystem(const string& prefix, const string& cmd)
{
infoLogger(cmd);
FILE* fpipe = popen(cmd.c_str(), "r");
if (fpipe == NULL)
throw std::runtime_error(string("Can't run ") + cmd);
char* lineptr;
size_t n;
ssize_t s;
do {
lineptr = NULL;
s = getline(&lineptr, &n, fpipe);
if (s > 0 && lineptr != NULL) {
if (lineptr[s - 1] == '\n')
lineptr[--s ] = 0;
if (lineptr[s - 1] == '\r')
lineptr[--s ] = 0;
infoLogger(prefix + lineptr);
}
if (lineptr != NULL)
free(lineptr);
} while (s > 0);
int status = pclose(fpipe);
infoLogger(String::Format("Status:%d", status));
return status;
}