954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C programming from linux command line

Command line arguments in C
===========================
#include
int main(int argc,char *argv[])
{
FILE *fp;
char c;
if(argc==2)
{
fp=fopen(argv[1],"w");
while((c=getchar())!=EOF)
{
putc(c,fp);
}
fclose(fp);
fp=fopen(argv[1],"r");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
}
return 0;
}

How to run this program in linux?
I have tried these steps:

1. open terminal in linux
2. type the command ./a.out program_name file_name

rajina
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

if program name is vi sample.c means then for run ./sample

sdinu96
Newbie Poster
13 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Did you compile the code ?
Did you specify the name of the executable when you were compiling the code ?If not the default name is a.out

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

to compile the code, (assuming you have gcc installed, if you aren't and are on ubuntu, a handy shortcut is running sudo apt-get install build-essential)

run gcc -o my_program my_program.c

then, run ./my_program

As a matter of interest, vim is a useful editor in these circumstances.

A few formatting notes: c source is usually in a name.c file (.cpp for C++), headers in a .h file. INDENT your code (for your own sake). Post your code INDENTED (by using the [code] button in the post editor) for our sake.

Best of luck.

sirio.bm
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

to compile the code, (assuming you have gcc installed, if you aren't and are on ubuntu, a handy shortcut is running sudo apt-get install build-essential)

run gcc -o my_program my_program.c

then, run ./my_program

As a matter of interest, vim is a useful editor in these circumstances.

A few formatting notes: c source is usually in a name.c file (.cpp for C++), headers in a .h file. INDENT your code (for your own sake). Post your code INDENTED (by using the [code] button in the post editor) for our sake.

Best of luck.

Command line arguments in C
===========================

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fp;
char c;
if(argc==2)
{
fp=fopen(argv[1],"w");
while((c=getchar())!=EOF)
{
putc(c,fp);
}
fclose(fp);
fp=fopen(argv[1],"r");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
}
return 0;
}


I have given name of the program cmdEx.c
And saved in one of the directory ,then opened terminal in the same directory.
typed command ./a.out cmdEx.c file.txt
Its working...
Thank you for your kind suggestions

./a.out program_name.c file_name.txt

rajina
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You