Hi,
I'm having a problem switching to another file once a certain amount has been read. Once volSize has been read, I want to open the next file (in a volume set) and continue reading data from it. Here is the code.
Code:
void Volume(char *fileToOpen)
{
FILE* fp;
FILE* out;
size_t result;
size_t totalRead = 0;
size_t bytesToRead = 0;
size_t GlobRead = 0;
int numFiles;
int volume = 1;
int volSize;
int i;
int finishVol = 0;
int bufsize = 4096;
char buffer[bufsize]; // 4 kb
char volName[256];
sprintf( volName, "%i-%s", volume, fileToOpen);
fp = fopen(volName, "rb");
if(fp == NULL) Error("No file handle to bin");
// Read in general data
ReadInfo(fp, &numFiles, &volSize);
printf("Number of files: %i\nVolume: %i MB\n\n", numFiles, volSize/1024/1024);
struct file file[numFiles];
// Read in file specific data for all files
for(i = 0; i < numFiles; i++)
ReadHeader(fp, &file[i].fileName, &file[i].fileNameLen, &file[i].fileSize);
printf("Gathered all file information\n\n");
/* Now begin writing out each file */
for(i = 0; i < numFiles; i++)
{
out = fopen(file[i].fileName, "wb");
if(out == NULL) Error("No file handle");
printf("File %i: %.2f MB\n", i + 1, (double)file[i].fileSize / 1024 / 1024);
totalRead = 0;
bytesToRead = 0;
do
{
bytesToRead = file[i].fileSize - totalRead;
if(bytesToRead > sizeof buffer)
bytesToRead = sizeof buffer;
result = fread(buffer, 1, bytesToRead, fp);
if(result <= 0) Error("No bytes read\n");
totalRead += result;
GlobRead += totalRead;
WriteChunkBin(out, buffer, result);
if(GlobRead + bufsize >= volSize) // Code to switch volume
{
finishVol = (GlobRead + bufsize) - volSize;
result = fread(buffer, 1, finishVol, fp);
if(result <= 0) Error("No bytes read\n");
totalRead += result;
WriteChunkBin(out, buffer, result);
fclose(fp);
free(volName);
finishVol = 0;
GlobRead = 0;
volume++;
sprintf( volName, "%i-%s", volume, fileToOpen);
fp = fopen(volName, "rb");
if(fp == NULL) Error("No file handle on volume\n");
totalRead = 0;
}
}
while(totalRead < file[i].fileSize);
printf("Wrote file %i: %s\n\n", i + 1, file[i].fileName);
// Repeat for all files
fclose(out);
}
fclose(fp);
printf("Success!");
}
For example, let's say the volume size is 50 mb. When 50 mb has been read in total, regardless of however many files have been read, it should switch to the next file and continue reading the data. This should continue until all the data has been read from a multi-volume set.
Thank you for you time and help.
EDIT: Here is the code for splitting files into volumes in case it matters.
Code:
out = fopen(outName, "ab");
if(out == NULL) Error("No file handle\n");
for(i = 0; i < numFiles; i++)
{
// Open file
fp = fopen(argv[i + minArg - 1], "rb");
if(fp == NULL) Error("Error opening file\n");
fileSize = GetFileSize(fp);
printf("File %i: %.2f MB\n", i + 1, (double)fileSize / 1024 / 1024);
do
{
result = fread(buffer, 1, sizeof buffer, fp);
if(result <= 0) break;
WriteChunkBin(out, buffer, result);
written = written + (int)result;
if(written >= volSize)
{
fclose(out);
free(outName);
volume++;
sprintf( outName, "%i-%s", volume, argv[2]);
out = fopen(outName, "wb");
if(out == NULL) Error("No file handle\n");
written = 0;
}
}
while(result == sizeof buffer);
printf("Wrote file %i: %s\n\n", i + 1, argv[i + minArg - 1]);
// Repeat for all files
fclose(fp);
}
fclose(out);