Saturday, 18 December 2010

Program To reverse the first n characters in afile

Description:

This program perform the reverse operation of n characters in the file



Program:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>

void main(int argc, char *argv[])
{
Char a[15], s[20],n;

int k,j=0,I,len;

FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r"); if(fp == NULL)

{
puts("File cannot be opened.");
exit(0);
}

k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)

{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}

Output:


source.c
this is source ouput.c

Command line arguments source.c    ouput.c

source.c
this is source text
output.c
this is source text

Command line arguments source.c
Invalid number of arguments.

Conclusion: the program is error free


VIVA QUESTIONS:


1) List out the file handling functions ?
Ans: fopen(), fprintf(),fclose(),fscanf(),fgetc(),fputc(), etc..,

2) What is the use of fseek() function ?
Ans: The function fseek sets the file pointer associated with a stream to a new position

3) What is use of the fflush() function ?
Ans: If the given stream has a buffered output, fflush writes the output of the stream to the associate file.

Files

No comments:

Post a Comment