Wednesday, April 2, 2014

File IO Using Main Arguments

First, compile the code. 
#include<stdio.h>
main( int argc, char *argv[]) {
FILE *output = fopen(argv[1], "w");
fputs("Roses are red,\n", output);
fputs("Violets are blue,\n", output);
fputs("Rhyming is overrated,\n", output);
fputs("Zebra.\n", output);
fclose(output);
}
view raw gistfile1.txt hosted with ❤ by GitHub

Using the command prompt, the first argument is the file name. 
While the second argument is the name of the output file you want.
You may also specify the destination path of the file output on the second argument.

Method 1:
This will create the file poem.txt on the same folder where I saved my fileio9.c 


Method 2:
This on the other hand will save the poem.txt on the desktop




This is from my Intro to Programming Class (ANSI-C). I'll try to post some of my code snippets here. Hope I'll be able to do it. 

No comments:

Post a Comment