I have a file I want to append data to and I have used the following line to try to move right before EOF (but I suspect that it isn't necessary, I might make a simple test and post back):
Code: Select all
fseek(file, -1, SEEK_CUR);
Here's the rest of the code and the full program (create_big_file.c) is at the start of the post, on the link surrounded by two Floppy-alike images:
Code: Select all
//Open appending to continue expanding file:
///
fHandle=fopen(creatFileName, "ab+");
//We opened the file for appending, so try to move
//1 byte right before EOF:
///
fseek(file, -1, SEEK_CUR); //Move before EOF...
Full code:
Code: Select all
#include <stdio.h>
//NOTE: These two libraries seem to need to be together
// to avoid an error message that says:
//
// "warning: incompatible implicit declaration of built-in
// function 'memset'":
///
#include <stdlib.h>
#include <string.h>
//This is a macro to easily access the first command line
//as the file name the program should use as a big file
//to create:
///
#define creatFileName argv[1]
#define sectors512Count argv[2]
//(bytes_per_sect*sects)-zero_addressing
//
//for completing aligned/full file bytes
///
#define creatFileSize____512MB (512*1048576)-1
#define creatFileSize____20GB0 39070080
#define creatFileSize____875GBytes 939524096000
#define creatFileSize____8754ksects 229376000
#define creatFileSize____875GB512sects 1835008000
int main(int argc, char *argv[])
{
//Here we just declare the variables
//to handle the simple big file to create:
///
void *buff;
FILE *fHandle;
const char *fName = "hard_disk.img";
long int numsects = creatFileSize____512MB;
if(argc<3)
{
printf("ERROR: Please specify a file name and a number of 512-byte sectors");
printf("\r\n");
return;
}
//Let's open (and create or overwrite)
//the specified file name:
///
//if(!fHandle=fopen(creatFileName, "wb+")) //Create new file...
if(!fHandle=fopen(creatFileName, "ab+")) //Append, continue expanding file...
{
printf("ERROR: Couldn't open %s\r\n", creatFileName);
return -1;
}
if(!(abs(numsects=atol(sectors512Count))))
{
printf("ERROR: Sector count (%ld) must be greater than 0\r\n", numsects);
return -2;
}
//We opened the file for appending, so try to move
//1 byte right before EOF.
//
//These lines are unnecessary since the pointer
//will be set at the end of file on the first
//fwrite (in this case) or fread
//(see http://f.osdev.org/viewtopic.php?f=13&p=258018#p258018)
///
//fseek(file, -1, SEEK_CUR); //Move before EOF...
//fseek(file, 0, SEEK_SET); //Move to start... optional...
//Reserve and clear a 512-byte buffer:
///
buff=malloc(512);
memset(
buff, //Buffer to zero out
0, //(int)(unsigned char) value to write (0 in this case)
512 //Number of bytes to write
);
//Write as many 512-byte buffers as the requested
//size in 512-byte sectors:
///
do
{
fwrite(buff, 1, 512, fHandle);
}
while(--numsects);
free(buff);
fclose(fHandle);
return 0;
}