C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 8th, 2012, 03:02 AM
q1cet0o q1cet0o is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 q1cet0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old September 8th, 2012, 03:40 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 37 sec
Reputation Power: 1800
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.

Reply With Quote
  #3  
Old September 9th, 2012, 01:49 AM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old September 9th, 2012, 03:41 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 37 sec
Reputation Power: 1800
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.

Reply With Quote
  #5  
Old September 9th, 2012, 06:07 AM
pratster pratster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 8 pratster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old September 9th, 2012, 08:02 AM
q1cet0o q1cet0o is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 q1cet0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 6 sec
Reputation Power: 0
tnx a lot

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Now working program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap