Oct 13, 2011

C Program to reverse the first n characters in a file.

C Program to reverse the first n characters in a file using the command line arguments. Here we read the file name and n are specified on the command line. If file exists, it reverse the n characters, else it gives the error message. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


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

void main(int argc, char *argv[])
{
 char a[15];
 char s[20];
 char n;
 int k;
 int j=0;
 int i;
 int 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();
}/*(Note: The file name and n are specified on the command line.)*/

Read more Similar C Programs
File operations

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!