The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Now working program
Discuss Now working program in the C Programming forum on Dev Shed. Now working program C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 8th, 2012, 03:02 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
Time spent in forums: 1 h 39 m 6 sec
Reputation Power: 0
|
|
|
Now working program
hello, i'm not familiar with the programming, but i'm must become beacause i have to go in for an examination
Here's my problem: this program is written in my textbook, but the only result i receive is this message: "Usage: find <filename> <ch>". Is this all that must be displayed?
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
if(argc!=3){
printf("Usage: find <filename> <ch>\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("Cannot open file.\n");
exit(1);
}
while((ch=fgetc(fp))!=EOF)
if(ch==*argv[2]){
printf("%c found",ch);
break;
}
fclose(fp);
return 0;
}
|

September 8th, 2012, 03:40 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by q1cet0o Is this all that must be displayed? | No. If you think that you are doomed as far as any examination is concerned. Most examinations occur at the end of a course of lessons - how come you have got to the examination without the lessons!?
Read the code, or just follow the instructions. The message output is telling you how to run the code. It is obviously asking you to provide a filename and a character as command line arguments. That is not a programming issue, that is a fundamental aspect of using a computer.
If you are running the code from the command line, you need to provide two arguments, e.g.:
find somefile.txt x
then if somefile.txt exists and contains a 'x' character, a "found" message will be output. If somefile.txt does not exist, the "cannot open" message will be output.
If you are running the code from some IDE, it will probably have a place in the project or debug configuration where you can add test arguments.
|

September 9th, 2012, 01:49 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation Power: 0
|
|
|
Note your Argument Count (argc) also takes in the count of your Program File Name.
Ex: if the file in which this program is written is named Program.c.
Your Command line should look like
"Program.c filename.txt x" here your argc is 3.
In simple argc also takes in the profram file name as its input!!
Hope this helps you to determine the output.
|

September 9th, 2012, 03:41 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by pratster
Ex: if the file in which this program is written is named Program.c.
Your Command line should look like
"Program.c filename.txt x" here your argc is 3.
|
C is not a scripting or interpreted language - you cannot run a .c file! An executable generated from a sourcefile called Program.c can have any name, of which Program.c is most unlikely!
It is clearly implicit in the question the executable name in this case is "find" so this is just an inaccurate and unnecessary duplicate of my answer.
|

September 9th, 2012, 06:07 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 8
Time spent in forums: 1 h 11 m
Reputation Power: 0
|
|
|
Oops! Yeah i didn't notice that "find" must be an executable on the cmd interface!
Misinterpreted it to be the argument(filename)!
Thanks for letting me know.
|

September 9th, 2012, 08:02 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
Time spent in forums: 1 h 39 m 6 sec
Reputation Power: 0
|
|
tnx a lot 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|