what is the difference between binary and txt modes in C++ [duplicate] - c++

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!)

Related

How to take unbuffered input in c++ (i.e without hitting enter key)? [duplicate]

This question already has answers here:
synchronous keystroke reading from console application
(3 answers)
Closed 3 years ago.
I'm trying to take comma delimited input from the user so that without pressing enter key,the input is accepted.
I think that's not possible in a portable manner in pure C++, because it depends too much on the terminal used that may be connected with stdin (they are usually line buffered). You can, try to use getchar() or getline() and try to tweak your code based on your requirement.

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

how to find size /memory space occupied by a "process" [duplicate]

This question already has answers here:
How can I measure the actual memory usage of an application or process?
(31 answers)
Closed 8 years ago.
I was asked to write a c/cpp program to find size of any process in an interview. can any one tell me how this could be achieve ?
p.s.- before marking the que as duplicate - plz read it carefully : I have asked how to find via c/cpp program not just with any unix/linux shell command
You can make use of getrusage. But keep in mind that it is not implemented on all systems.
Or by reading the /proc/[pid]/statm
Otherwise, try one of these (command line options).
It's not part of standard C++ and thus depends on the operating system.
On linux for example that is done by accessing /proc filesystem.
Another option is of course to just call a system command like ps and parse its output (that is is what I'd do in a Python script).
Being able to interpret the numbers you can get is however another non trivial problem.
Use
size <executable>
Output
text data bss dec hex filename
1361623 1984 2708 1366315 14d92b <executable>
It shows text, data, bss and total size

Portable end of line (newline)

It's been an unpleasant surprise that '\n' is replaced with "\r\n" on Windows, I did not know that. (I am guessing it is also replaced on Mac...)
Is there an easy way to ensure that Linux, Mac and Windows users can easily exchange text files?
By easy way I mean: without writing the file in binary mode or testing and replacing the end-of-line chars myself (or with some third party program/code). This issue effects my C++ program doing the text file I/O.
Apologies for the partial overlap with other answers, but for the sake of completeness:
Myth: endl is 'more portable' since it writes the line ending depending on the platform convention.
Truth: endl is defined to write \n to the stream and then call flush. So in fact you almost never want to use it. All \n that are written to a text-mode stream are implicitly converted to \r\n by the CRT behind the scenes, whether you use os<<endl, os<<'\n', or fputs("\n",file).
Myth: You should open files in text mode to write text and in binary mode to write binary data.
Truth: Text mode exists in the first place because some time ago there were file-systems that distinguished between text files and binary files. It's no longer true on any sane platform I know. You can write text to binary-opened files just as well, you just loose the automatic \n -> \r\n conversion on Windows. However, this conversion causes more harm than good. Among others, it makes your code behave differently on different platforms, and tell/seek become problematic to use. Therefore it's best to avoid this automatic conversion. Note that POSIX does not distinguish between binary and text mode.
How to do text: Open everything in binary mode and use the plain-old \n. You'll also need to worry about the encoding. Standardize on UTF-8 for Unicode-correctness. Use UTF-8 encoded narrow-strings internally, instead of wchar_t which is different on different platforms. Your code will become easier to port.
Tip: You can force MSVC to open all files in binary mode by default. It should work as follows:
#include <stdio.h>
#include <iostream>
int main() {
_fmode = _O_BINARY;
std::ofstream f("a.txt"); // opens in binary mode
}
EDIT: As of 2021, Windows 10 Notepad understands UNIX line endings.
The issue isn’t with endl at all, it’s that text streams reformat line breaks depending on the system’s standard.
If you don’t want that, simply don’t use text streams – use binary streams. That is, open your files with the ios::binary flag.
That said, if the only issue is that users can exchange files, I wouldn’t bother with the output mode at all, I’d rather make sure that your program can read different formats without choking. That is, it should accept different line endings.
This is by the way what any decent text editor does (but then again, the default notepad.exe on Windows is not a decent text editor, and won’t correctly handle Unix line breaks).
If you really just want an ASCII LF, the easiest way is to open the file in binary mode: in non-binary mode \n is replaced by a platform specific end of line sequence (e.g. it may be replaced by a LF/CR or a CR/LF sequence; on UNIXes it typically is just LF). In binary mode this is not done. Turning off the replacement is also the only effect of the binary mode.
BTW, using endl is equivalent to writing a \n followed by flushing the stream. The typically unintended flush can become a major performance problem. Thus, endl should be use rarely and only when the flush is intended.

How to Check if File is ASCII or Binary in C++ [duplicate]

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.