Thread: How to write file .txt which can read-only.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Lightbulb How to write file .txt which can read-only.

    I use Visual C++ 6.0. My source is below. I want to write text file which can read-only. If you know how to edit source to do that ,please tell me. Thank you very much.

    #include <stdio.h>
    int main()
    {
    FILE *outp;
    char *output_filename;

    output_filename = "output.txt";
    outp = fopen(output_filename, "w");

    fprintf(outp, "1");

    fprintf(outp, "\n");

    if (output_filename)
    {
    fclose(outp);
    }
    }

    PS. I really want to create logfile but I do not know how to do.So I create text file instead. Does text file (read-only) and logfile have much difference?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Just a tip, use the [ code ] tag.

    Code:
    #include <stdio.h> 
    int main() { 
       FILE *outp; 
       char *output_filename; 
    
       output_filename = "output.txt"; 
       outp = fopen(output_filename, "w"); 
     
       fprintf(outp, "1"); 
    
       fprintf(outp, "\n"); 
    
       if (output_filename) { 
          fclose(outp); 
       } 
    }

  3. #3
    Unregistered
    Guest
    you cant make a file trully readonly... so your prob better off encrypting it...

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    100
    How do you encrypt it?

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    with alogithorims Did I spell that right?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208

    XOR Will Work

    If it is just a log file and isn't overly important then XOR encrytion will do quite nicely. It really shouldn't be used for very secure info but as it sounds it would work good for you cause u want sumthing simple.

    XOR Encryption works like this

    A B A XOR B
    T T F
    T F T
    F T T
    F F F

    so your code should look sumthing like this

    Code:
    #include <iostream.h>
    
    int main()
    {
      char string[6]="Hello";//your text you want encrypted
      char key[6]="ABCDE";//you always need a key
      for(int x=0; x<10; x++)
      {
        string[x]=string[x]^key[x];//this loop does the actual encrytion
        cout<<string[x];                //whick i explained below
      } 
      return 0;//end your program
    }
    The symbol for XOR is ^ the actual algoithm for XORing text is
    'c=a^b' c=decryted file a=file you want encrypted and b= your key. To decrypt you just do this 'a=b^c'.

    If you are a little lost there is a good tutorial located here http://www.cprogramming.com/tutorial/xor.html

    Some people like to play with the XOR encryption technique to make it more secure. But if the text in your log isn't the most important thing in the world then simple XORing should be fine for you

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    you can set the read-only flag. in dos the command is "attrib +r filename.ext". you can try a system call like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. read and write to file
    By staticalloc in forum C Programming
    Replies: 17
    Last Post: 09-20-2004, 05:57 PM