How can I replace 12 byte data from a local file?

108 views Asked by At

My App would download a file which the first 12 byte data was encrypted,so I have to read this file from Document and decrypt the first 12 byte data.

My first try was to read the whole file and decrypt it then write to file again. but if the file is too large , this will cost a lot of memory.

So ,Is there anyway to let me read the first 12 byte data and replace it?

3

There are 3 answers

0
user1627167 On BEST ANSWER

This is a std way of doing in any language.

Read a file in chunk Replace in Buffer write in Temporary file delete the original file and rename temporary file as original file.

As far as objective C concerned i found a useful link

http://www.techotopia.com/index.php/Working_with_Files_in_Objective-C

goto this topic "Writing Data to a File"

1
AudioBubble On
#include <stdio.h>

FILE *f = fopen("yourFileName", "rb");
if(f == NULL)
    ; // handle exception
int nBytesToRead;
char theBuffer[nBytesToRead];  
size_t bytesRead = fread(theBuffer, 1, nBytesToRead, f);
// do with your bytesRead
fclose(f);
0
Varun Singh On

Well Objective-C is based on C and hence all the functions including file-operation functions should work out-of-the-box. You can convert the NSString file-path to char array and perform the desired file operations with the help of the this link.