Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
* * * * - 1 votes

Simple C File Handling In Action


7 replies to this topic

#1 Nitin Mangi

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 19 posts

Posted 15 March 2006 - 11:49 PM

Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all formalities to get the code review incorporated and update all source code files with code review headers.

Imagine if you need to open 300 files one by one and append code review headers at the end.

Since most files are reviewed in groups of 20 to 30 files. We require one header to be placed in say 20 to 30 files.

To simplify I went back to my class assignment days and wrote this small c utility to open all files passed on command line and open attach code review headers and close them.

We also need to checkout and check in from source control application, but that’s easy scripts.
So just plugged in this utility in batch file and complete the tiring work in seconds.

Its simple c file handling in action. I thought of posting it here for my friends who want to learn c file handling most features in a very small program.

Or for somebody like me who requires to complete his code reviews and is lazy enough to write this. So it’s all action.

Its simple you can just modify this skeleton as you like it.

#include "stdio.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{	
	/* pointers to file for writing and file containg the header */
	FILE * fpWrite; 
	FILE * fpReviewHeader;
	/* size of file and header */
	long lSizeOfHeader = -1;
	long lSizeOfFile = -1;
	/* buffer to keep the header to be appended */
	char * buffer;

	/* at least one file must be passed */
	if( argc < 2 )
	{
		printf("\nUsage: acrh <filename>\n");
	}

	/* open file have the header in read and binary mode 
	   you make like to take this also from command line also.
	   This file starts with a new line character used
	   in case the file where header will be appended does
	   not have new line character in the end */
	fpReviewHeader=fopen("c:\\temp\\review.txt", "rb");

	/* get the file size */
	fseek (fpReviewHeader , 0 , SEEK_END);
	lSizeOfHeader = ftell (fpReviewHeader);
	rewind (fpReviewHeader);
	
	/* allocate memory to contain the whole header file.*/
	buffer = (char*) malloc (lSizeOfHeader);
	
	/* read the file into the buffer. */
	fread (buffer,1,lSizeOfHeader,fpReviewHeader);	
	
	/* get all the file names passed on the command line*/
	for(int i=1;i<argc;++i)
	{
		/* open the each file in append and binary mode */
		fpWrite=fopen(argv[i], "ab+");
		/* here we get the size of the file and check if the 
		   second last character is new line character	*/
		fseek (fpWrite , 0 , SEEK_END);
		lSizeOfFile = ftell (fpWrite);
		rewind (fpWrite);
		fseek (fpWrite , lSizeOfFile-2 , SEEK_SET);
		int aChar = fgetc(fpWrite);
		fseek (fpWrite , 0 , SEEK_END);
		/* if no newline is there we append the original buffer with new line	*/
		if( aChar != 13 )
		{
			fwrite(buffer,1,lSizeOfHeader,fpWrite);			
		}
		/* remove the newline character from buffer */
		else
		{
			/* write the block header to file */
			fwrite(buffer+2,1,lSizeOfHeader-2,fpWrite);		
		}
		printf("\nUpdated: %s",argv[i]);
		/* this flushes the data from cache to file */
		fflush(fpWrite); 
		/* close this file */
		fclose(fpWrite);
	}
	/* close the header file */
	fclose(fpReviewHeader);
	/* free the header buffer */
	free(buffer);

	return 0;
}

Enjoy :(

#2 jmb2006

    Member [Level 3]

  • Kontributors
  • PipPipPipPipPipPip
  • 96 posts

Posted 17 March 2006 - 10:06 PM

cool, im learning c myself, how long did it take you to write that code? also what websites did you learn from, decent websites for c programming beginners? thanks.

#3 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 09 April 2008 - 03:17 PM

file programs in c
Simple C File Handling In Action

Actually I m selected for 3rd round in cmy.
In technical round they ask to write some program in file concept in c, malloc and c alloc program so I need some programs which are used to write in technical round

-reply by pavithra

#4 moodsey211

    Newbie [Level 3]

  • Kontributors
  • PipPipPip
  • 40 posts
  • Gender:Male
  • Location:Cebu City Philippines

Posted 14 July 2008 - 02:24 AM

cool code. but can it still be trimmed down? to make the code a little more smaller.

#5 nickthat

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 04 October 2008 - 09:10 PM

I should point out a small bug in your code.

if( argc < 2 )
{
printf("\nUsage: acrh <filename>\n");
}

You need a return statement in the "if" block. probably returning a non zero error value which is the standard way of doing it in C.

#6 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 06 November 2008 - 04:46 AM

How to modify a file?
Simple C File Handling In Action

I am unable to modify a file...I want to change a character in the file but not at the end of file...Please help me...

-reply by kartiki

#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 20 November 2009 - 04:57 PM

File Handling in C/C++Simple C File Handling In Action

Hi,

I'm trying to create a  file Expenses.Txt, Open the file and copy intoAnother  Expenses2.Txt through Dev C++, can anybody figure how to do  this?

Thanks..

-reply by zahid pervaiz

 



#8 Guest_super_*

  • Guests

Posted 04 July 2011 - 10:13 AM

how to convert a c program with comments to get the result as a same program, but without comments
using file concept mechanism




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users