0

I created a File of 4000 blocks with a blocksize of 4096 Bytes. I wrote to some specific blocks in this file and now I want to read these blocks and write the result into a new output file.

Therefore I open the file I created in "rb" mode (storeFile) and the outputfile in "wb" mode (outputFile) as follows:

FILE * outputFile=fopen(outputFilename,"wb");
FILE * storeFile=fopen(storeFilename, "rb");

now I am trying to seek for the right position and read all blocks to the new file (outputfile):

for(i=0; i<BlocksUsed; i++)
{
    fseek(storeFile,blocksToRead[i]*4096,SEEK_SET);
    fread(ptr, 4096,1,storeFile);
    fwrite(ptr,4096,1outputFile);
    ...
    rewind(storeFile)
 }

unfortunately this code leads to a file which is not the file I wrote to the storeFile. The files' size is BlockUsed*4096Bytes.

What am I doing wrong?

Thank you in advance!

SevenOfNine
  • 630
  • 1
  • 6
  • 25
  • what is blocksToRead array and what is in it? – Richard Chambers Aug 17 '13 at 22:23
  • there are the numbers of blocks in which I wrote earlier. – SevenOfNine Aug 17 '13 at 22:25
  • 1) I don't see where you're checking for "file open" or "file read" errors, 2) you probably do NOT need either "fseek()" or "rewind()" *inside* of your loop – paulsm4 Aug 17 '13 at 22:28
  • if you are using a direct access file with fseek() no reason to use rewind() which is really for sequential files. What is it that you are actually wanting to do with this code? If the outputfile is empty and you are reading BlocksUsed number of 4096 blocks then why wouldn't the outputfile end up with the same number of blocks? – Richard Chambers Aug 17 '13 at 22:28
  • The files are opened without problems. I check this but did not post this. Hmm how comes I dont need fseek? How do I come to my position then? If I need to read BLock 2 5 and 10 for example? – SevenOfNine Aug 17 '13 at 22:30
  • well Its kind of an archive. I am writing files into it and want to get them out again. The problem is that not every file is x*4096 bytes. So if I wrote a 512byte File into one block I expect a 512 Byte file to come out again. – SevenOfNine Aug 17 '13 at 22:32
  • Could you modify your question to explain what it is that you are wanting to do? It sounds like from your comment that you want to take multiple separate files and combine these multiple files into a single file. If this is the case then there are some changes that you will need to make to the outputfile structure. The idea of blocks is ok however I suggest that there should be struct composed of a length, an unsigned short would do, followed by the series of bytes which may be from 0 bytes in length to the maximum size of the buffer. So each block indicates the number of actual bytes. – Richard Chambers Aug 17 '13 at 23:11
  • So `blocksToRead[]` contains potentially non-sequential block numbers in the source file that you want to pull in, writing them in 0..n-1 order in the output file. Does that about sum it up ? That's what is seems like you're trying to do anyway. – WhozCraig Aug 17 '13 at 23:11

2 Answers2

0

Here's a silly example, but it might help illustrate a few points:

#include <stdio.h>

int
main (int argc, char *argv[])
{
  FILE *fp_in, *fp_out;
  int c;

  /* Check command-line */
  if (argc != 3) {
    printf ("EXAMPLE USAGE: ./mycp <INFILEE> <OUTFILE>\n");
    return 1;
  }

  /* Open files */
  if (!(fp_in = fopen(argv[1], "rb"))) {
    perror("input file open error");
    return 1;
  }

  if (!(fp_out = fopen(argv[2], "wb"))) {
    perror("output file open error");
    return 1;
  }

  /* Copy bytewise */
  while ((c = fgetc(fp_in)) != EOF)
    fputc (c, fp_out);

  /* Close files */
  fclose (fp_out);
  fclose (fp_in);
  return 0;
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0
 fseek(storeFile,blocksToRead[i]*4096,SEEK_SET);
 int n = fread(ptr, 4096,1,storeFile);
 if (n <0)
 {printf("read error\n");return -1;}
 if(fwrite(ptr,n,1outputFile) != n)
 {printf("write error\n"); return -1;}
    ...
 //rewind(storeFile)// no need . since you use fseek 
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31