Miscellaneous Code#
Creating files with Test Code#
Sometimes we need to create files with text to help us develop or test our code.
Bash or CMD#
One ways to create a file in the command line is to use echo
echo "some text" > filename.txt
system() function#
Caution
This method is not intended for production code
You can wrap the command in function
system()to run a command from your code.system("echo \"some text\" > filename.txt");
You can also use
sprintfto build you command string using variables.sprintfworks similarly toprintfexcept it sends the data to the a variable instead to the screen.char *filename = "filename.txt"; char command[128]; // container for the command // Build a string and store in variable 'command' sprintf(command, "echo \"%s\" > %s", "some random text", filename); // Execute the command system(command);
Source & license
Reproduced verbatim, without modification from © 2022, BilimEdtech Labs, licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
Source page: https://labs.bilimedtech.com/operating-systems/references/misc.html
See LICENSE for the full license text.