how does this work

Pages: 12
closed account (N8MNAqkS)
this doesn't do anything. it does not open a txt file and print anything in it.

1
2
3
4
	std::ofstream myfile;
	myfile.open("Array.txt", std::ios::out | std::ios::binary);
	myfile << "Writing this to a file.\n";
	myfile.close();
it works fine here..
are you using an IDE? Some ide, the path is unclear... this should write in the same folder where it was invoked from (which may or may not be where the exe is). run it directly from the console, in the folder where the exe is, then look there for your file, and if that works, its just a pathing issue .. youll have to figure out where it puts the file when you run it the other way.
Visual Studio created apps have a different default folder location, depending on if you run the app from within the IDE or where the executable is created (debug or release folder).

If run via the IDE the file you want to open should be located where your source files are located.

If executing the app from the folder where the .exe is located the file to open should be there.

IOW, you should have two identical copies of your input file.
closed account (N8MNAqkS)
How do I run it from the console in the folder where the exe is, i cant find the folder is the problem. But on top of that how do I open a console in the folder.
closed account (N8MNAqkS)
Eventually when I get this working i want to put code into it and have it stored as binary, that way I can kind of make a secure save system.

Which means I have to learn how to get the code that I write translated into its binary form and saved that way Inside of the file, and then when I load it up it needs to be translated back into cpp. That way I can have values change and instead of resetting every time the program terminates, values will stay the same. Because they won't die at the end of the program, instead the values will be moved somewhere that they can be reloaded next time I run the program
Last edited on
How do I run it from the console in the folder where the exe is

1. Use Window's File Explorer.

i cant find the folder

With your project open in Visual Studio the project properties window (bottom on the right) shows the folder where the project file is located. Above that folder should be a Debug or Release folder if you've already successfully compiled your source. Select that folder and view the contents. Your app's .exe file should be there.

how do I open a console in the folder.

The address bar in File Explorer. Select it and highlight the entire address. Type "cmd," (without quotes) and press enter.

You should now have a console window open with the location DOS style already set. Drag and drop your exe into the console window. That drops the DOS style address of your executable.

Hit enter and your console app runs in the already opened console window. When the app finishes the console window stays open.
I highly recommend you take a moment off and learn basic console skills. They are extremely valuable to programmers. Dos and unix are similar enough that you can learn enough of both pretty fast.


https://www.digitalcitizen.life/command-prompt-how-use-basic-commands


both dos and unix support redirect to and from a file (eg, a text file of inputs for testing your code, or a text file out output so you can search your output in a text editor for long runs full of debug messages etc). that looks like this in both OS:

programname > output.txt //overwrites
programname >> output.txt //appends
programname < input.txt


in unix, you have to type
./programname to run a program unless you have put it in specific folders or added the path where your executables land to allow it without the ./ (its a bit dumb, but that is how it is).

extending just a tiny bit more
dir /s will search forward into folders to find stuff, eg
dir /s array.txt would find your file (eventually).

* means anything.
dir *.txt means all the .txt files will be shown.
? is one character.
dir ?rray.txt will find array.txt and brray.txt and so on if they exist.

both dos and unix support building small mini-programs from these commands (batch or shell script respectively) -- super useful for programmers, for example you can make a dos batch file that compiles your code with standard options for your compiler.
Last edited on
closed account (N8MNAqkS)
FINALLY new stuff to learn. Thx for the answers.

But one more thing. Do any of you know how to change cpp code into its binary form? This would be helpful.
When you compile cpp code, the compiler takes the text file and produces a binary object file.
closed account (N8MNAqkS)
my properties tab is empty. I cant find it from there.

but I changed the file from txt to bin and I was able to find it in the file explorer. unfortunately to view I need to download an app that is capable of viewing bin files but it seems to work.
Last edited on
When you said "binary form", what did you mean? Every file is in binary form, so people generally don't just mean "a file". So what did you mean, when you said "binary form"? Do you mean "where is the cpp source code file saved?"
Last edited on
Right.

Explain again what you intend to do with this binary file?
my properties tab is empty. I cant find it from there.

The Solution Explorer window in VS must be the active window, and have the project name selected.

If you use the VS defaults it will be something like Project1 or some other number.

If you chose a name for the project, select that name.
you can look at 'binary' files with a program called a 'hex editor' … visual studio used to have one built into it, but maybe no longer. It will show you the file in hex. If you open a "text" file with it, you can see spaces and end of lines etc in the raw too.

to write a binary file, you need to use the write command. lets try it:
int x[] = {2,4,8,255,65535}; //some stuff you should recognize in hex.
myfile.write(x, sizeof(int)*5);

you need to work on stating explicitly what you want. Its hard to describe what you don't yet know, sure, but try. I had to infer the question, so like the others, not sure if this is what you wanted to see, but guessing from both your words and your code...

<< (unless overloaded) is for text. binary is written in blocks of bytes and lacks the 'special case' rules that text has for specific bytes.

The dos stuff can wait if you feel overloaded.
but learning just 5 or so commands would save you a ton of energy as a coder.
cd. dir. del. xcopy. ren. and execution of a program. "paths with spaces need quotes\place" . Just those, when you get a chance, are huge.
Last edited on
closed account (N8MNAqkS)
what I intend to do with this file, or at least what I wanted to do I know I feel it is impossible, was to write executable code to it, with values that could change like coordinates or any variable with changeable values, then I want to run the program, have the values change, and then save the new values to the bin file. this way I can have the compiler reload the file, this time with its new values, so every time I run the program variables will not die when the program terminates, and end up resetting to their original values when I run the program again. this way I can make a save system.

I made a diagram to show you what I mean but there is no way for me to show you. this is the one forums in the whole world that wont let you drag and drop pictures onto the text box.




Last edited on
The typical way people do that sort of thing is by having a separate file for storing data in, and reading/writing to that file. A "save file", if you will.
closed account (N8MNAqkS)
Ok so what i mean by binary form is, well I'm not gonna act like I know exactly how it works, but from what I understand, every piece of code has a binary form, that is what a compiler default does, it translates languages like c++ into something the computer can understand, in general it's binary. I thought I binary file was actually binary, like 100110. Or however it looks.
But apparently that is not what bin files do.

I was trying to make a save file, but the only thing you can do it seems with ifstream is write text to a file, i cant put executable code into it.


I wanted the code to be transferred into the file in binary format (100110 BS) because it is more secure that way. If I were to use this method in a game it would be harder to read and change the executable code if someone were to hack the game and try to mod it. Because unless you are a computer i imagine reading 100110 110010 110110 101010 so on and so forth, is pretty damn hard.

You can write binary to a save file using ofstream:

https://codeyarns.com/2013/12/27/how-to-write-binary-file-in-c/

If you want to write more complex things than basic primitive types, the search term you're looking for is "serialization".
ooo. Ok, Let me preach on it all.

- binary is indeed 1s and 0's but at the *disk* level. a disk is (or was, ssd is different) little bits of magnetic material that are either "left or right" in their physical position that represents 1 and 0 when the reader passes over them. Computers deal with binary in 8 bit chunks (bytes) so most 'binary' that we 'see' is hexidecimal because its easier to read and mess with.

- all files are binary. text files are binary files that follow some extra rules, is all. they are a subset of {all binary files} which is redundant for {all files period}.

- all code is converted to a binary form, sure. But that is machine language ... int x = 10 becomes some code that says stuff like ... move 10 into a cpu register (registers are hardware variables made up of some few bytes of raw data) followed by move the register to memory where x is stored (a hard-coded address like a pointer that represents 'x'). So your one line of readable text becomes something like 2 64 bit integers that represent these 2 commands to the CPU. The binary/bit/byte values of those 2 integers have a bit of info in them (what register, what memory addres, what value, etc) in various parts of the bit pattern. If you are still with me, this is why what you want to do is ... doable but extremely difficult and not the right way to do things due to the complexity. It would take forever to write and debug something at this level.

- you can put executable code in a binary file, of course you can... that is what a .exe or .dll etc file have inside. But you don't know what to write (neither do I) and due to the above, its not something you want to try to do unless your goal in life is to write compilers (someone has to do this, but its not for everyone).

- my example above clearly shows how to use (ofstream, ifstream only reads) to write a binary file. you use the .write() method instead of stream operator <<. If you just write text words into a binary file, you get a text file (its a subset see earlier point). Try doing what i said with the little integer array.

- binary is not more secure. Hackers can read it, I can read it. Ive reverse engineered many a file. Again, hex editors let you mess with code. When I was a KID I was editing executable files of games... gave myself more lives or whatever. Its not that hard, but its extremely tedious.

I mean if you want to play with it, compile a tiny program with a few KB size for the executable and we can write a little program that dumps that into a C++ readable file that makes a hard coded array of the bytes. Then we can write that back to disk in binary as a .exe and run it. Then we can modify something (say its hello world and we change it to some other string) and write that out and run it and see other text. Maybe doing that would show you why what you are thinking isnt practical to do...

what you should do is what was said... save your data in a binary file (encrypt it if you want to secure it) and read and write that, leave the executable alone. Even relatively simple xor encryption is pretty secure unless some hacker is really, really, really bored or your program is extremely popular. If its the next fortnite, you need professional encryption.
Last edited on
closed account (N8MNAqkS)
so your saying I went through the trouble of reading researching and ignoring school work so I could do this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int  x = 0, y = 0, z = 0;
	
	std::fstream Coordinates;

	Coordinates.open("Position.exe", std::ios::in | std::ios::binary);
	Coordinates.write((char*)x, sizeof(int));
	Coordinates.write((char*)y, sizeof(int));
	Coordinates.write((char*)z, sizeof(int));
	Coordinates.close();

	std::cout << "enter a coordinate for X: \n";
	std::cin >> x;
	std::cout << "enter a coordinate for Y: \n";
	std::cin >> y;
	std::cout << "enter a coordinate for Z: \n";
	std::cin >> z;

    Coordinates.open("Position.exe", std::ios::out | std::ios::binary);
    Coordinates << x;
    Coordinates << y;
    Coordinates << z;
    Coordinates.close();


and its all for nothing.
Last edited on
Pages: 12