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


This method is not intended for production code

  1. You can wrap the command in function system() to run a command from your code.

    system("echo \"some text\" > filename.txt");
  2. You can also use sprintf to build you command string using variables. sprintf works similarly to printf except 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