Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple fseek question

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
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);
}
}
 
I don't see why you need to use fseek() here. fread() advances through the file automatically without the need for fseek.

This is how you copy a file 256 bytes at a time
Code:
FILE *sourcefile_fp, *targetfile_fp;
char source_file[256], move_file[256];
char buf[256];
size_t readsize;

if(!(sourcefile_fp = fopen(source_file, "rb")))
  return(1);
if(!(targetfile_fp = fopen(move_file, "wb")))
  return(1);
while ( ( readsize = fread(buf, sizeof(char), sizeof(buf), sourcefile_fp ) ) != 0 ) {
  fwrite(buf, sizeof(char), readsize, targetfile_fp);
}

--
 
I had no idea fread did it automatically, thanks! Now, on that note, I want to parse this file out into multiple files, and I have '****' as a file read stop, so everytime it encounters this string in the file, I need to go back and write all that I have read so far into a new file, and so on until the end of file. Would I use fseek for this and ftell for the original starting position, or am I making it harder than it is?
 
If its a text file, you should be using fgets() rather than fread()



--
 
Main issue: you can open up to 256 files with fopen on most systems (Windows isn't most systems) and even more with open. The file pointer for each file will stay where you left it. There is no need to go back to the start every time. Not sure if I understood your question correctly.

Side issue: If you really did want to use fseek, use
Code:
   pos = 0;
   while(!feof)
    {
      int result;
      result = fseek(sourcefile_fp, pos, SEEK_SET);
      pos += 256;
      if(!(targetfile_fp = fopen(move_file, "a+b")))
        return(1);
fseek returns 0 if successful - not the current file position.
 
Code:
foo
foo
foo
****
bar
bar
bar
****
baz
zork
You want to split this text file into 3 separate files containing the relevant parts, as delimited by the ****
?


--
 
Salem,
That's true, I do want to parse into that many files, where the next file starts with '****'. The file, however, can be

foo foo foo**** bar bar**** blah blah

all on one line, not each individual line. I created a function that, when I encounter a '*', it will then go into the function and fgetc to find if there are another 3 *'s consecutively.

if(!(movefile_fp = fopen(move_file, "rb")))
return(1);

i = 0;
while(movefile_fp > 0)
{
cur_pos = ftell(movefile_fp);

byte_count = GetNextDotd(movefile_fp);

if(byte_count <= 0)
break;

fseek(movefile_fp, cur_pos, SEEK_SET);

if(!(newfile_fp = fopen(folder_filename, "wb")))
return(1);

numwritten = fwrite(&dotd, sizeof(char), file_size, file_fp);

fclose(newfile_fp);
}

fclose(movefile_fp);
}

static int GetNextStar(FILE *movefile_fp)
{
int char_read = 0, num_asterisk = 0;
char c;

c = ' ';
while ((c != EOF) && (num_asterisk < 4) && (char_read < sizeof(my_struct)))
{
c = getc(movefile_fp);
char_read++;

if(((c < 0) || (c > 255)) && ((char_read > 1) && (char_read < 257)))
c = ' '; /* get rid of eof characters in the header */

if((c == '*') && (char_read > 4))
num_asterisk++;

else
num_asterisk = 0;

if(c == '\0')
c = ' ';
}

if(char_read <= 4)
return(EOF);

else if(c == EOF)
return(char_read - 1);

else
{
/* back up 4 characters */
fseek(movefile_fp,-4,1);
return(char_read - 4);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top