Finding a specific file in a directory [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to copy text file in C or C++?
I need a code which specifically look up a directory and find a pre-specified file name then copy it to another folder. It sounds easy but couldnt do it. Can anbody help me?
thanks

Some functions you may find useful from WINAPI:
FindFirstFile()
FindNextFile()
CopyFile()
boost has a filesystem library which has a copy_file function.

Last time I checked, C++ doesn't offer a file copy function. You need to open the file, read in its data into a local buffer, then make a new file, and write the contents of the local buffer into the new file.
File IO Reference

Related

How do I get current file path in c++? [duplicate]

This question already has answers here:
How to get current source path in C++ - Linux
(2 answers)
Closed 2 years ago.
I want get current file path,like in a.cpp,I can get
/home/workspace/src/a.cpp
How can I get this path?
You can use the standard macro __FILE__ to which expands to a string literal that contains the path of the current source file.
Starting from C++20, you can use default constructed std::source_location.
I couldn't comment due to the reputation requirement.
But you might find this useful, a way to retrieve directory on Linux & Windows.
How do I get the directory that a program is running from?
Hope this help!

Reading the binary content of a file [duplicate]

This question already has answers here:
How do I read an entire file into a std::string in C++?
(23 answers)
Closed 8 years ago.
I have a university assignment and i need to have some sort of function that detects the presence of a binary sequence (in my case, it should be a "virus" signature -> the assignment is to create some sort of antivirus that checks files for viruses ) in a file. To do this i believe i need to get the whole binary content of the file, and check the presence of that signature. The problem is that every function i found gives me the content of the file, not the raw binary data. Any ideas how i should proceed with this? Thanks !
I think you would use: istream::read

Why use a C++ header file? [duplicate]

This question already has answers here:
Why have header files and .cpp files? [closed]
(9 answers)
Closed 8 years ago.
I understand the header file contains all the prototypes of every function contained in the implementation file; but what is the real purpose? Is it because when another program calls on the class, it only receives the information in the header file, and the implementation is kept hidden? What is the real purposes of this?
The purpose is that when someone gives you a library, they don't always give you the code. And frankly you don't want to know about it. You simply need to know the function prototypes and data structures it provides, which you obtain from the Header File. And then you link to the library.
Here's an example that might help make some sense of it for you:
I publish a library libfoo (doesn't matter if its a .so, .a or .dll) and told you it had a bunch of good functions and data type in it that solve a problem and you want to use it. If I didn't give you the header file, how would your code possibly compile when it would need to know how big data structures are / what function signatures to look for? How would you know how to code it?
The library won't be referenced until linking, and compilation is already complete at that point in time.

Reading from set of files [duplicate]

This question already has answers here:
How can I get the list of files in a directory using C or C++?
(33 answers)
Closed 9 years ago.
I am a newbie so please don't be harsh on me for asking simple questions. my main question is how can I read from several files located in a directory sequently using a loop in c++ and perform some actions on them?
My code is like this:
string corpus = "corpus.txt";
myfile.open(corpus);
if (myfile.is_open())
while (!myfile.eof())
{
//Do something
}//end of while
MergeFiles(corpus,count);`
How can I do this actions on a set of files instead of just one.
Looks like you could do one of the following:
use windows APIs
use third party code
It looks like the simplest way is using diren.h. Look for at the sample code here:
How can I get the list of files in a directory using C or C++?
you can use the boost libraries, more specific - the filesystem library. This is more powerful but complicated solution, requires you to use iterators and higher programming methods.
using the windows API is not recommended because it requires dipper understanding of windows, and won't result a portable code. If you want to use it, there is an example in MSDN.

Searching Directories "backwards" using windows API in C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the parent directory of the current folder in a C program?
I am using the C++ Windows API and I look for folders and directories given a specific directory path initially (i.e. C:\...\ProjectXX). I use in particular the functions FindFirstFile() and FindNextFile(). I want however sometimes to move "backwards" in a directory, namely to get the "parent" of the given directory (the folder which contains the directory given). In what way is this possible and under the use of what functions?
C++, unlike C, has a method for it: .remove_leaf(). But you're not using C++ functions, you're using WinAPI functions, so PathRemoveFileSpec() would be more fitting.