flnMichael
Programmer
I'm relatively new to fseek, so I really don't know if I'm using this call right. I want to move a file in chunks of 256 bytes at a time, but for some reason, I either get only 256 bytes to the file I'm writing to or an appending of all files into one. What am I doing wrong?
FILE *sourcefile_fp, *targetfile_fp;
char buf[256], move_file[256];
int file_size, numwritten;
if(!(sourcefile_fp = fopen(source_file, "rb")))
return(1);
/* Read the first record of the input.
*/
if((pos = fseek(sourcefile_fp, 0L, SEEK_SET)) == -1L)
return(1);
else
{
if((file_size = fread((char *)&buf, 1, sizeof(buf), sourcefile_fp)) <= 0)
{
fclose(sourcefile_fp);
return(1);
}
if(!(targetfile_fp = fopen(move_file, "a+b")))
return(1);
numwritten = fwrite(&buf, sizeof(char), file_size, targetfile_fp);
fclose(targetfile_fp);
while(!feof)
{
pos = fseek(sourcefile_fp, 256, SEEK_SET);
if(!(targetfile_fp = fopen(move_file, "a+b")))
return(1);
if((file_size = fread((char *)&buf, 1, sizeof(buf), sourcefile_fp)) <= 0)
{
fclose(sourcefile_fp);
return(1);
}
numwritten = fwrite(&buf, sizeof(char), file_size, targetfile_fp);
fclose(targetfile_fp);
}
}
FILE *sourcefile_fp, *targetfile_fp;
char buf[256], move_file[256];
int file_size, numwritten;
if(!(sourcefile_fp = fopen(source_file, "rb")))
return(1);
/* Read the first record of the input.
*/
if((pos = fseek(sourcefile_fp, 0L, SEEK_SET)) == -1L)
return(1);
else
{
if((file_size = fread((char *)&buf, 1, sizeof(buf), sourcefile_fp)) <= 0)
{
fclose(sourcefile_fp);
return(1);
}
if(!(targetfile_fp = fopen(move_file, "a+b")))
return(1);
numwritten = fwrite(&buf, sizeof(char), file_size, targetfile_fp);
fclose(targetfile_fp);
while(!feof)
{
pos = fseek(sourcefile_fp, 256, SEEK_SET);
if(!(targetfile_fp = fopen(move_file, "a+b")))
return(1);
if((file_size = fread((char *)&buf, 1, sizeof(buf), sourcefile_fp)) <= 0)
{
fclose(sourcefile_fp);
return(1);
}
numwritten = fwrite(&buf, sizeof(char), file_size, targetfile_fp);
fclose(targetfile_fp);
}
}