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 "some text" > filename.txt
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 to
printfexcept it sends the data to the a variable instead to the screen.
char *filename = "filename.txt"; char command; // 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);