Reputation: 83
For my project we are supposed to read a file and copy it in reverse. The part I am stumped on is how I can get the program to read the file off the command line, because I have no idea what file will be used to test this, and I don't have the given txt file. Is there a way to assign a variable to file that hasn't been declared yet and to create a new file that shows the reverse txt? Sorry if this is not clear, I will try to make it as clear as possible if you need me to.
Upvotes: 3
Views: 95
Reputation: 10516
From command line you can pass file to write some thing and file which will be reversed
Your program looks like
int main(int argc,char *argv[] )
{
//...
return 0;
}
compile
gcc program.c -o out
You need to pass files like this
./out file_to_write file_to_write_in_reverse
------------------------------------------------------------or---------------------------------------------------------------------
In Your Program
use fgets()
and read file name of file_to_write
You can create a file.
fopen()+ w+ mode
Write data, for example write string into file
`fprintf()` or `fputs()`
use fgets()
and read another file name of file_to_write_in_reverse
create file_to_write_in_reverse
fopen()+ w mode
and now Use fseek()
, fgetc()
and fputc()
Upvotes: 0
Reputation: 43
You pass the file name in using the argv[] array that is declared when you create main. Because fopen in C takes a char, or char array you can then open the file specified through the command line. For all the information you should need regarding this use the site I link.
http://www.cprogramming.com/tutorial/c/lesson14.html
Upvotes: 1