Originally Posted by
Dino
Just fseek to the offset you want and close the file.
The only problem I see with this is that it doesn't reserve space on the disk, using the code below I was able to write 10 files of 640MB each to a 1GB pen drive. This method creates sparse files correct?.. is there a way to reserve the space on the disk without writing all zeros to the file (Linux on a NTFS partition using ntfs-3g driver or mod of such)?
Code:
/*simple file IO test*/
#include <stdio.h>
FILE* getFile(const char*,int);
int main(void){
for (int i=0;i<10;i++){
char str[50];
int res = sprintf(str,"/mnt/destination/myTest%d.txt",i);
FILE *testFile = getFile(str,671088640);
if(testFile !=NULL){
fputs("good test",testFile);
fclose(testFile);
}else
printf("error creating file %d \n",i);
}
return 0;
}
FILE* getFile(const char* path,int sizeBytes){
FILE *pfile;
int result;
if(pfile = fopen(path,"w")){
fseek(pfile,sizeBytes,SEEK_SET);
fputs("\n",pfile);
fclose(pfile);
pfile = fopen(path,"r+");
fseek(pfile,0,SEEK_SET);
}
return pfile;
}