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
Related
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!
This question already has answers here:
How do I read an entire file into a std::string in C++?
(23 answers)
Closed 6 years ago.
I'm trying to learn Google's c++ compression library Snappy. It requires that data be loaded into an std::string to compress or decompress it. How can I go about opening a file and loading the data into an std::string. Thanks.
Check the Unit tests of Snappy library. There is a GetContent function that loads binary data into the string. Also you can check how it's used for data compression there.
This question already has answers here:
Difference between files written in binary and text mode
(7 answers)
Closed 7 years ago.
I began to use C++ recently,and this may seem to be a nieve queation but I couldn't find an answer for it.
When creating an fstream object, I have two options for mode, binary and txt.
fstream f ("file.txt",ios::out|ios::binary);
and
fstream f ("file.txt,ios::out|ios::binary);
both write the same strings when use the overloaded operator << . my question what is the differwnce between the two modes and does it affect the number of bytes used to write characters to the stream, so you will need a diifferent seekg when you read data written with each fstream ?
Certain special characters may get changed depending on what mode you are using.
Also, what those special characters get changed into may depend on the OS or computer system that the code runs on.
With binary files you are SURE that the file will be read as-is, on any computer and regardless of the contents of the file.
The difference in the kind of file IO says it all: Text mode is for text based files,
Binary is for all other kinds of IO (even text files if you don't want any interpretation to take place!)
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
This question already has answers here:
Closed 14 years ago.
So using the system command file we can use file to determine if a file is ASCII Text or "data". I wanted to know if there is a way to check in code which one it was? I want to basically throw a corrupt error if the file is 'data'. I am using ifstream for reading the files. Thanks for any help!
Duplicate of this question.
You can iterate over the bytes of the file and use std::isprint from <cchar> to test whether the character is printable. If there are nonprintable characters in the file, chances are it's a binary file. Notice that this only works for legacy encodings (e.g. ASCII mentioned by you), not for Unicode-encoded files.