Members BookmarksPolls Fresher Jobs Strange Photos Academic 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.

Advertisements


website counter



Programs on Files


Posted Date: 17 Jan 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: Sagaya James Immanuel       Member Level: Bronze
Rating:     Points: 5



1) /* Reverse a file(Reversing string by string) */

# include
# include
int i,j,k;
FILE *fp;
FILE *pf;
char ch;
char string[81];
char string1[81];
main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Incorrect Format");
getch();
exit(1);
}

if ( (fp = fopen(argv[1], "r")) == NULL)
{
printf("%s Cannot Be Opened ", argv[1]);
getch();
exit(1);
}

if ( (pf = fopen(argv[2], "w")) == NULL)
{
printf("%s Cannot Be Opened ", argv[2]);
getch();
exit(1);
}
clrscr();
while (fgets(string,80,fp) != NULL)
fputs(strrev(string), pf);
}

--------------------------------------------------------------------------------

2) /* Reverse the contents of a file(Charater by character using recursion) */

/*Program to Reverse the entire content of the file.*/
#include
#include
void main()
{
FILE *fp;
clrscr();
fflush(stdin);
fp = fopen("Memory.txt","r");
if(fp == NULL)
{
printf("\nCannot open the File");
}
else
{
if(fp)
{
rev(fp);
}
}
getch();
}
void rev(FILE *fp)
{
char c;
if ((c=fgetc(fp) != EOF)
{
rev(fp);
putchar(c);
}
}

--------------------------------------------------------------------------------

3) /* Counting number of words in a file */

#include
#include
void main(int argc, char *argv[])
{
FILE *fp;
char ch, string[81];
int white = 1;
int count = 0;

if (argc != 2)
{
printf("Format : file5 ");
exit(1);
}

if ( (fp = fopen(argv[1], "r")) == NULL)
{
printf("Cannot open file %s", argv[1]);
exit(1);
}

while ((ch=getc(fp)) != EOF)
{
switch(ch)
{
case ' ':
case '\t':
case '\n':
white++;
break;
default :
if (white)
{
white = 0;
count ++;
}
}
}
fclose(fp);
printf("File %s contains %d words", argv[1], count);
}

--------------------------------------------------------------------------------

4) /* Reading a file */

#include
#include
int main(int argc, char *argv[])
{
FILE *fp;
char string[81];

if (argc != 2)
{
printf("Format : file8 filename");
exit(1);
}

if ( (fp=fopen(argv[1], "r")) == NULL)
{
printf("Cannot open file %s ", argv[1]);
exit(1);
}
while (fgets(string,80,fp) != NULL)
printf("%s", string);
fclose(fp);
return(0);
}

--------------------------------------------------------------------------------

5) /* Writing a file */

#include
#include
void main()
{
int table[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

FILE *fp;

if ( (fp=fopen("table.rec", "w")) == NULL)
{
printf("Cannot open agents.rec");
exit(1);
}

fwrite(table, sizeof(table), 1, fp);

fclose(fp);
}

--------------------------------------------------------------------------------

6) /* Copy a file */

#include
int main()
{
FILE *in, *out;
int key;
if ((in = fopen("junk.txt", "r")) == NULL)
{
puts("Unable to open the file");
return 0;
}
out = fopen("copy.txt", "w");
while (!feof(in))
{
key = fgetc(in);
if (!feof(in))
fputc(key, out);
}
fclose(in);
fclose(out);
return 0;
}

--------------------------------------------------------------------------------

7) /* Database using files (structure and files) */

#include
#include
void main()
{
struct student
{
int roll;
char name[40];
} stu;

char numstr[81];

FILE *fp;
int recno;
long int offset;

if ( (fp=fopen("agents.rec", "r")) == NULL)
{
printf("Cannot open agents.rec");
exit(1);
}

printf("Enter record number : ");
scanf("%d", &recno);

offset = (recno - 1) * sizeof(stu);

if (fseek(fp, offset, 0) != 0)
{
printf("Cannot move pointer ");
exit(1);
}

fread(&stu, sizeof(stu), 1, fp);
printf("Roll Number %d\n", stu.roll);
printf("Name %s\n", stu.name);
fclose(fp);
}




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: Overview of Files Concept.
Previous Resource: VENDOR CERTIFICATION COURSES IN IT
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

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