Thread: Help implementing file modifcation algo.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    Help implementing file modifcation algo.

    Hello i m relatively new to programming and i want to implement the following algorithm.

    1. accept the directory path at command prompt
    2. open the directory and scan it for files of my interest (.drg)
    3. when the matching file is found scan it untill the @ character is found at newline.
    4. remove that whole line untill the EOF and save the file.

    So far i have successfuly coded the first 2 steps and also i can detect if the matching file ( with extension .drg ) is found but i cant figure out how to proceed. I would like to know the functions matching best for my need and i would try to code the next steps myself , as that will give me some good practice.

    And i have one more question :
    what is the best way to modify the file and save it ?

    I guess , for my specific task i willl have to copy all the contents untill newline and @ character is found and save it to another temp file. Then delete the original file and rename temp file to original filename . Is this approach the best ?

    Regards
    EnLi

    Edit : forgot to add my working environment ..which is DOS from XP and i m using Dev cpp as my compiler .
    Last edited by enli; 05-04-2008 at 11:34 PM. Reason: Wanted to add OS conditions !1

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I guess , for my specific task i willl have to copy all the contents untill newline and @ character is found and save it to another temp file. Then delete the original file and rename temp file to original filename . Is this approach the best ?
    This is the only portable approach

    there are some other solutions for truncating files platform dependent
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    5
    Hi.. could you please point me to those truncating solutions ?
    I googled it but i couldnt find them .

    Also i need some ideas on how to implement my algorithm steps 3 and 4

    Thanks
    EnLi

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    5
    thanks vart .. no offence but i could search for file truncate.. but i m necessarily interested in my solution to my current problem .

    Never mind i will figure out that myself
    Last edited by enli; 05-05-2008 at 11:34 PM. Reason: typo

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by enli View Post
    thanks vart .. no offence but i could search for file truncate.. but i m necessarily interested in my solution to my current problem .

    Never mind i will figure out that myself
    And the third item on that search is this, which would be the right solution on Linux/Unix:
    http://linux.die.net/man/2/truncate

    Adding "windows" to the search gives this as the first hit :
    http://discuss.fogcreek.com/askjoel/...74&ixReplies=7

    If you are using a different platform, then perhaps appending that would help you out.

    You may not get the FULL story from those links, but it should at the very least be sufficient to ask further questions in more detail.l

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    5
    Thanks matsp.. i also got that page in my previous queries.. but i didnt look the sencond webpage thorughly. It seems that the function SetEndOfFile() can do that.

    I m looking about that ..and i will keep you updated.

    Much appreciated
    EnLi

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    5
    Ok ..the following sample program does what i was looking for . Hope this might help some needy.

    Code:
    /*
    *   Title        : File truncation
    *   Description  : This little program shows how to truncate a file .
    *   Should work on XP(tested), LINUX(not tested)
    *
    *   test.txt file contents were : aaabaaaasdasdasd
    *   after running the program file contents were : aaab    
    *    and file size currectly reported
    *   
    */
    
    #include <stdio.h>
    #include <unistd.h>  // for ftruncate()
    //#include <conio.h> // for getch()
    int main()
    {
        FILE *fp;
        char ch;
        long int file_descriptor;
        int currpos;
        if((fp=(fopen("test.txt","r+")))== NULL) // file should be opened with write permission
            perror("File Read error");
        
        while((ch=getc(fp))!='b')           // move fp untill 'b' is found
        {}
             
        file_descriptor=fileno(fp);       // get file descriptor
        currpos=ftell(fp);                // get the desired file size
        
        //Setting new file size
        if(ftruncate(file_descriptor,currpos) == 0)
            printf("\n\nFile succesfuly truncated");
         else
            perror("File truncation ERROR : ");       
        fclose(fp);
        //getch();
        return 1;
    }
    Last edited by enli; 05-06-2008 at 05:13 AM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

Tags for this Thread