Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Simple C File Handling In Action, Small code snipet which covers most of basic file handling and navigat
Rating 4 V
Nitin Mangi
post Mar 15 2006, 11:49 PM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 19
Joined: 11-March 06
Member No.: 19,842



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.

CODE

#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 smile.gif
Go to the top of the page
 
+Quote Post
jmb2006
post Mar 17 2006, 10:06 PM
Post #2


Member [Level 3]
******

Group: Members
Posts: 96
Joined: 7-July 05
Member No.: 9,135



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.
Go to the top of the page
 
+Quote Post
iGuest
post Apr 9 2008, 03:17 PM
Post #3


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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
Go to the top of the page
 
+Quote Post
moodsey211
post Jul 14 2008, 02:24 AM
Post #4


Newbie [Level 2]
**

Group: Members
Posts: 35
Joined: 10-July 08
From: Cebu City Philippines
Member No.: 64,841



cool code. but can it still be trimmed down? to make the code a little more smaller.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. C Code, Can U Solve This?(22)
  2. Weird Characters Appending To File(2)
  3. Adding Myfileexists Function To My Basic Code?(2)
  4. Reactions To A Busy File(2)
  5. Code Documentation(1)
  6. Storing Bit Values In A File(1)
  7. Local File Time To File Time()(0)
  8. Handling Byte Streams(2)
  9. Prob With My C Code(4)
  10. Source Code For Paint Like Program Under Dos In C(2)
  11. File Format Encyclopedia Release 1.00(2)
  12. Wolfenstein Source Code Now In Public Domain(3)
  13. Encrypting A File(5)
  14. A Telephone Directory Source Code In C(2)
  15. Buffer Overflow In Action Tutorial(0)
  1. C++ Roman Numeral Conversion Code Help(5)
  2. Most Efficient Code To Get Prime Numbers(7)


 



- Lo-Fi Version Time is now: 26th July 2008 - 08:09 AM