read and write to file

This is a discussion on read and write to file within the C Programming forums, part of the General Programming Boards category; Originally Posted by Prelude >You might also want to make sure you close the file properly. It certainly couldn't hurt ...

  1. #16
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Prelude
    >You might also want to make sure you close the file properly.
    It certainly couldn't hurt if you really need robust code or if you don't flush the stream before closing it and a write error occurs. At that point you're looking at a loss of data situation, and recovery is critical. For toy programs like we write here, you just need to think of a call to fclose as a placeholder for something more solid.
    can't help it you guys are turning me into a pedant

    So as to the problem the op is having. I've been playing around and came up with some code. It seemed to have worked. So in trully annoying style i'll just post the logic here. You guys can let me know if you see anything wrong with my pseucode.
    Code:
    begin:
        
        type_t encrypt(int) //whatever type your return value is
        
        FILE *file1,*file2;
        int ltr,code;
        
        open file1 for reading:
            do error handling
        open file2 for writing:
            do error handling
            
        while ( (ltr =fgetc(file1)) !=EOF):
            code = encrypt(ltr)
            fputc(code,file2)
            
        if (both file1 && file2 are not close):
            give error message
            
        remove file1
            
        end
    The basic idea is to have two files open at the same time. Seems depending on the OS you can have 10 to about 20 files open at the same time.
    one file is only for reading which contains the original message, the other is for writing.
    you might want to make a function to encrypt your data at least for modularity purposes.
    once you copied the changed data to the second file, close both files like so
    Code:
    if (fclose(file) != 0 || fclose(file2) != 0)
            fprintf(stderr,"Error in closing files\n");
    and then remove the first file with the original message. Which is obviously not really deleted, simply removed the link to it.

    Wish i knew more about files right now, but Just started. let me know how it goes. I've done it alright. I'm sure there is an easier way. Like Dave_sinkula's but i don't know that yet.

  2. #17
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    I added fflush to my code and it works!!! YES! It feels so good!

    thanks guys

  3. #18
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Static post your code so i can see it please.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  4. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21