how exclude initial part of a buffer???

S

Senger

Hello!
I have to take a file and exclude the inicial part (88 positions) to a
buffer.

I try with this code, but not functioning:

fp = fopen(file,"rb");
fseek(fp,0,SEEK_END);
size = ftell(fp);
data = malloc(size);
fseek(fp,88,SEEK_SET);
fread(data,size,1,fp);
fclose(fp);

Where data containg the file without 88 initial positions....

Thanks for help!!
 
S

Senger

Sorry by this messagem. I can´t edit the first topic.

I forgott to say: The file mentioned above is a binnary file.

Tks...
 
M

Manish

fp = fopen(file,"rb");
fseek(fp,0,SEEK_END); <--I think the problem is right here, at this
instance, you move to end of the file.

size = ftell(fp);

Perhaps you can use "rewind(fp);"

data = malloc(size);
fseek(fp,88,SEEK_SET);
fread(data,size,1,fp);
fclose(fp);

Just on the side note, I didn't see any error checking for null
pointers. Perhaps you didn't mention it while posting it.

For reference you take a look at following link:
http://www.cplusplus.com/ref/cstdio/fread.html
 
S

Shezan Baig

Senger said:
Hello!
I have to take a file and exclude the inicial part (88 positions) to a
buffer.

I try with this code, but not functioning:

fp = fopen(file,"rb");
fseek(fp,0,SEEK_END);
size = ftell(fp);
data = malloc(size);
fseek(fp,88,SEEK_SET);
fread(data,size,1,fp);


This line is the problem. Since you are at position 88, you can only
read "size-88" bytes into 'data'.

Hope this helps,
-shez-
 
S

Senger

I tried to do something like this:

fp = fopen(file,"rb");
fseek(fp,0,SEEK_END);
size = ftell(fp);
data = malloc(size);
rewind (fp);
fseek(fp,88,SEEK_SET);
fread(data,tamanho-88,1,fp);

But not function....
Do I have another form to do that??
How I can exclude the 88 bytes from data after pass the values from
this variable??

I don't have problems when I use the iostream and fstream, like this:

ifstream file;
file.open(filename, ios::in|ios:binary|ios::ate);
size = file.tellg();
file.seekg (0, ios::beg);
file,seekg(88, ios::beg);
file.read (buffer, size);
file.close();

But I can't use the iostream or fstream. I have to use the stdio.h.

Thanks for help!!
 
S

Senger

I using the script to use a modified file.
But I have to exclude 88 bytes of the begining from file on my system.
In second mode, using a ifstream, I don't find any problems.
But when I use the first script (stdio) the values passed to buffer is
the complete file, and not the partial file, without 88 first bytes.

If I set the position of pointer using "fseek(fp,88,SEEK_SET);"
the "fread(data,1,size,fp)" it would not have to read after the 88th
byte???
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

With the folloing program "try.cc" I can not reproduce your problem:

// 3456789abcdefgh
#include <cstdio>
#include <cstdlib>

const char* const file = "try.cc";
const long offset = 8;

int main()
{
FILE* fp = fopen("try.cc", "rb");
fseeko(fp, 0, SEEK_END);
long size = ftell(fp);
char* data = (char*)malloc(size);
fseek(fp, offset, SEEK_SET);
fread(data, 1, size-offset, fp);
printf("%c\n", data[0]);
}

Running the program yields "8" as excepted.
You should check the return value of all called functions
to track down the problem.

Regards, Stephan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top