Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



File Handling programs of C


Posted Date: 14 Jun 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: Lenin       Member Level: Diamond
Rating:     Points: 1



File Handling programs

This section contains programs demonstrating file handling and command
line arguments.

The following program reads a text file and counts how many times each
letter from 'A' to 'Z' occurs and displays the results.

#include
#include
#include

int count[26];

void main(int argc, char *argv[])
{
FILE *fp;
char ch;
int i;
/* see if file name is specified */
if (argc!=2) {
printf("File name missing");
exit(1);
}

if ((fp= fopen(agv[1], "r")) == NULL) {
printf("cannot open file");
exit(1);
}

while ((ch=fgetchar(fp)) !=EOF) {
ch = toupper(ch);
if (ch>='A' && ch<='Z') count[ch-'A']++;
}

for (i=0; i<26; i++)
printf("%c occurred %d times\n", i+'A', count[i]);

fclose(fp);
}

This program uses command line arguments to read and display the
contents of a file supplied as an argument.
#include

#define CLEARS 12 /* constant */

main(int argc , char *argv[])
{
FILE *fp , *fopen();
int c;

putchar(CLEARS);

while ( --argc > 0 )
if ((fp=fopen(argv[1], "r"))==NULL)
{
printf("I can't open %s\n", argv[1]);
break;
}
else
{
while ((c= getc(fp)) !=EOF)
putc(c,stdout); /* display to the screen */
fclose(fp);
}
}

This program gives a further example of the use of argc & argv
#include
#include

main(int argc , char *argv[])
{
double a , b;

if (argc != 3) {
printf("Usage: add number number ...\n");
exit(1);
}

a = atof(argv[1]);
b = atof(argv[2]);

printf("%lf\n" , a + b);
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Structure programs in
Previous Resource: Complex programs combining sections
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources

Watch TV Channels



Contact Us    Editors    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.