What is the ^# symbol at the end of my output txt file? - c++

I wrote c++ code to read from a text file and then bash code to output it to another file (specifically './executable &> output.txt'). When I print it on the command line it looks fine, but when I check the output file, it has a '^#' symbol at the end of it.

You have a bug in your program, it's writing a nul character at the end of your file. Then whatever tool you use to check the output is using: caret notation for non-printable characters.
^# is the notation for the nul character.
We cannot tell more without seeing the code.

Related

Attempt to compare character in string to carriage return does not work

I have some code to read a text based file format in that it checks for empty line with:
line == ""
where line is a string that receives a text line obtained through getline.
It worked with my own text based file format, but it did not work with another text based file format (not mine)
I opened the file with gedit and saw nothing. More and less utilities also did not show anything. Then I tried vi and it showed:
^M on all these lines that seemed empty until now (a screenshot of it is here: .
Did some research and it seems that opening the file in text mode, all I needed to do was to compare it to '\n'. So I wrote the line:
if (line[0] == '^M' || line[0] == '\n')
break;
to end a while loop where this "if" is inside, but it did not work. What do I need to do?
As you have already surmised, those ^Ms are vi's way of showing you that there are carriage return characters at the end of each line. The file probably originated on Windows.
As other commentators have mentioned, the way a carriage return character is represented in C / C++ is '\r', and the line endings in that particular file will almost certainly actually be \r\n (CRLF).
So, now you know how it all works you have some code to write. getline will remove the \n but you'll have to strip the \r (if there is one) off the end of the line yourself. Go to it.

C++ in Windows I can't put the Enter character into a .txt file

I made a program wich use the Huffman Coding to compress and decompress .txt files (ANSI, Unicode, UTF-8, Big Endian Unicode...).
In the decompression I take characters from a binary tree and I put them into a .txt in binary mode:
Ofstream F;
F.open("example.txt", ios::binary);
I have to write into .txt file in binary mode because I need to decompress every type of .txt file (not only ANSI) so my simbols are the single bytes.
On Windows it puts every simbol but doesn't care about the Enter character!
For example, if I have this example.txt file:
Hello
World!
=)
I compress it into example.dat file and I save the Huffman tree into another file (exampletree.dat).
Now to decompress example.dat I take characters from the tree saved in exampletree.dat and I put them into a new .txt file through put() or fwrite(), but on Windows it will be like this:
HelloWorld!=)
On Ubuntu it works perfectly and saves also the Enter character!
It isn't a code error because if I print in the console the decompressed .txt file, it also prints the enter characters! So there is a problem in Windows! Could someone help me?
Did you try opening the file using a wordpad or any other advanced text editor(Notepad++) which identify LF as newline character. The default editor notepad would put it in a single line like you described.
This may not be the solution you are looking for. But the problem looks to be due to having LF as the line break instead of windows default CR/LF.
It looks like it will be the difference in handling EndOfLine on Linux vs. Windows. The EOL can be just "\n" or "\r\n" - i.e. Windows usually puts 0x0d,0x0a at the end of lines.
On Windows there's a difference between:
fopen( "filename", "w" );
fopen( "filename", "tw" );
quote:
In text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output

How do I open a file within notepad++ with C++?

I'm trying to get my c++ program to open an sql file in notepad++. I can get it to open with notepad like this:
system("notepad.exe script_foo.sql");
But that's undesirable as it's not formatted. When I try to substitute notepad.exe for notepad++.exe like this:
system("'C:\Program Files\Notepad++\notepad++.exe' script_foo.sql");
I get a invalid syntax error.
Any issues where I'm going wrong?
The WinNT shell uses double-quotes to include spaces in a file name. Single quotes are not recognized. So you need
"C:\Program Files\Notepad++\notepad++.exe" script_foo.sql
as your command.
To embed this in C++ source code, you'll need to escape backslashes (as Andre already mentioned) and also the double-quotes.
system("\"C:\\Program Files\\Notepad++\\notepad++.exe\" script_foo.sql");
In C++, the backslash character \ is an escape character in strings. You need to double the backslashes to achieve what you really want:
system("'C:\\Program Files\\Notepad++\\notepad++.exe' script_foo.sql");

Hex values in gdb input files

I'm trying to bof a particular exploitme on DVL by redirecting input (to gets) using run < inputfile inside gdb
I can overflow the program successfully but am having trouble appending hex values to the string.. I have tried quotations, converting the value of the mem addr to ascii and various escape attempts (\,\,\) with no luck
Input file example:
AAAA\x42
In the above example it would appear that the backslash is being read as an ascii char (5c) and the value 42 remains in the stack (oddly?).
How would one go about specifying a hex value inside a gdb input file?
Thanks
Use perl! :)
reader#hacking:~/booksrc $ ./overflow_example $(perl -e 'print "A"x30')
with the 'e' option perl will evaluate the following command, and surrounding everything will treat the output of perl as a string. So the command above is identical to:
reader#hacking:~/booksrc $ ./overflow_example AAAAAAAAAAAAAAAAAAAAAAAAA
(adding x30 after a string will repeat it 30 times).
Of course perl accepts other hex values with the notation \x??. One more word, to concatenate strings use a dot:
reader#hacking:~/booksrc $ perl -e 'print "A"x20 . "BCD" . "\x61\x66\x67\x69" ;'
AAAAAAAAAAAAAAAAAAAABCDafgi
So you can redirect the output of perl in your input file or directly call perl in gdb when you run the program.

Extracting text from binary file like .exe/.dll etc using C++

I want to parse the given binary files and extract the information (text) from that using C++.
What methods are available?
You might use strings(1) to extract the strings of printable characters in files to a file or pipe, then process these lines. For example:
$ strings werl.exe
!This program cannot be run in DOS mode.
Rich
.text
`.rdata
#.data
.rsrc
QRVh
Could not load module %s.
win_erlexec
Could not find entry point "win_erlexec" in %s.
Could not find key %s in section %s of file %s
Cannot find erlexec.exe
erts-*
\bin
erts-
To save this output to a file out.txt, you use redirection:
$ strings werl.exe > out.txt
You can scan for strings of printable characters - most bytes in a binary code are non-printable, so when there is and uninterrupted string of for example 6 or more printable characters, there is good chance it is real string value. Plus strings are often terminated with \0, so you can look for a string of printable characters terminated by \0.