Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > streams binary and text

Reply
Thread Tools

streams binary and text

 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-29-2012
Do the functions fread and fwrite specifically deal with binary data and
not text? Can fgetc and fputc be used to deal with binary or are they only
for text streams. I know *nixs do not discriminate in texual or binary data
but the 'b' in fopen statements for opening streams is portabile across
platforms. But with a linux can I alter the data of an AVI? It is binary
isn't it and I would want to open a stream and use fprintf would I with the
%x conversion.

Bill


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-29-2012
Don't make my brown eyes China Blue wrote:
> In article k1jr5h$aha$,


[snip]

Whether
> the resulting file is still an AVI depends on whether your changes
> conform to the AVI format.


Say the file signature of an AVI is RIFF which represents four bytes. I
have to find where that is in the stream with for example fseek. How would I
do that? Simple enough with a hex editor but lives streams is harder.

Bill


 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      08-29-2012
בתאריך יום רביעי, 29 באוגוסט 2012 03:27:46 UTC+1, מאת Bill Cunningham:
>
> Say the file signature of an AVI is RIFF which represents four bytes. I
> have to find where that is in the stream with for example fseek. How would I
> do that? Simple enough with a hex editor but lives streams is harder.
>

/*
find a tag / string in a stream.
Params: fp - pointer to open file
tag - return for tag.
Returns: 0 = no tag in file, 1 = seek to tag , -1 error or some sort
Notes: Not tag structure aware, so some tags may be be spurious.
Notes: untested
*/
int seektag(FILE *fp, char *tag)
{
int ch;
char *buff;
int N;
long pos;

N = strlen(tag);
buff = malloc(N);
if(!buff)
return -1;
while( (ch = fgetc(fp)) != EOF)
{
if(ch == buff[0])
{
ungetc(ch, fp);
pos = ftell(fp);
if( fread(buff, 1, N, fp) != N)
{
free(buff);
return -1;
}
if(!strncmp(buff, tag, N))
{
fseek(fp, pos, SEEK_SET);
free(buff);
return 1;
}
}
}
/* weve got to the end without finding the tag */
free(buff);
if(ferror(fp))
return -1;
return 0;
}

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-29-2012
On Wed, 29 Aug 2012 04:27:43 -0700 (PDT), Malcolm McLean
<> wrote:

>?????? ??? ?????, 29 ??????? 2012 03:27:46 UTC+1, ??? Bill Cunningham:
>>
>> Say the file signature of an AVI is RIFF which represents four bytes. I
>> have to find where that is in the stream with for example fseek. How would I
>> do that? Simple enough with a hex editor but lives streams is harder.
>>

>/*
> find a tag / string in a stream.
> Params: fp - pointer to open file
> tag - return for tag.
> Returns: 0 = no tag in file, 1 = seek to tag , -1 error or some sort
> Notes: Not tag structure aware, so some tags may be be spurious.
> Notes: untested
>*/
>int seektag(FILE *fp, char *tag)
>{
> int ch;
> char *buff;
> int N;
> long pos;
>
> N = strlen(tag);
> buff = malloc(N);
> if(!buff)
> return -1;
> while( (ch = fgetc(fp)) != EOF)
> {
> if(ch == buff[0])


At this point, isn't buff[0] indeterminate during the first iteration
of the while loop? buff doesn't receive a value until the fread
below.

> {
> ungetc(ch, fp);
> pos = ftell(fp);
> if( fread(buff, 1, N, fp) != N)
> {
> free(buff);
> return -1;
> }
> if(!strncmp(buff, tag, N))
> {
> fseek(fp, pos, SEEK_SET);
> free(buff);
> return 1;
> }
> }
> }
> /* weve got to the end without finding the tag */
> free(buff);
> if(ferror(fp))
> return -1;
> return 0;
>}


--
Remove del for email
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-29-2012
Malcolm McLean <> writes:

> בתאריך יום רביעי, 29 באוגוסט 2012 03:27:46 UTC+1, מאת Bill Cunningham:
>>
>> Say the file signature of an AVI is RIFF which represents four bytes. I
>> have to find where that is in the stream with for example fseek. How would I
>> do that? Simple enough with a hex editor but lives streams is harder.
>>

> /*
> find a tag / string in a stream.
> Params: fp - pointer to open file
> tag - return for tag.
> Returns: 0 = no tag in file, 1 = seek to tag , -1 error or some sort
> Notes: Not tag structure aware, so some tags may be be spurious.
> Notes: untested
> */
> int seektag(FILE *fp, char *tag)


A note the anyone learning C: Malcolm objects to both "const" and
"size_t" so this function won't work with const char * arguments
(without a cast) and it contains a lot of needless signed/unsigned int
conversions. I'd add const above and declare N below as size_t.

(Malcolm: I'm not trying to convert you, but I think your opinion is
singular enough to merit being pointed out.)

> {
> int ch;
> char *buff;
> int N;
> long pos;
>
> N = strlen(tag);
> buff = malloc(N);
> if(!buff)
> return -1;
> while( (ch = fgetc(fp)) != EOF)
> {
> if(ch == buff[0])


Typo: you mean tag[0] of course.

> {
> ungetc(ch, fp);
> pos = ftell(fp);
> if( fread(buff, 1, N, fp) != N)
> {
> free(buff);
> return -1;
> }
> if(!strncmp(buff, tag, N))
> {
> fseek(fp, pos, SEEK_SET);
> free(buff);
> return 1;
> }
> }
> }
> /* weve got to the end without finding the tag */
> free(buff);
> if(ferror(fp))
> return -1;
> return 0;
> }


I think this is working too hard. I'd match as we go, mainly because it
avoid using a buffer. I think the result is clearer:

int seektag(FILE *fp, const char *tag)
{
int ch;
size_t match = 0, tlen = strlen(tag);
long pos;

while (match < tlen && (ch = fgetc(fp)) != EOF)
if (ch == tag[match]) {
if (match == 0) {
ungetc(ch, fp);
pos = ftell(fp);
fgetc(fp);
}
match += 1;
}
else match = 0;

if (match == tlen) {
fseek(fp, pos, SEEK_SET);
return 1;
}
if (ferror(fp))
return -1;
return 0;
}

I'd be inclined protect against empty tag strings, but your code did not
so I copied that decision.

It's even simpler if you are prepared to rely on SEEK_CUR (i.e. a binary
stream):

int seektag(FILE *fp, const char *tag)
{
int ch;
size_t match = 0, tlen = strlen(tag);

while (match < tlen && (ch = fgetc(fp)) != EOF)
if (ch == tag[match])
match += 1;
else match = 0;

if (match == tlen) {
fseek(fp, -(long)tlen, SEEK_CUR);
return 1;
}
if (ferror(fp))
return -1;
return 0;
}

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary vs. formatted std::string reading/writing to streams Leslaw Bieniasz C++ 2 01-15-2010 11:53 PM
text and binary streams subramanian100in@yahoo.com, India C Programming 4 08-24-2008 11:14 PM
Efficient processing of binary data streams in Ruby? theosib@gmail.com Ruby 2 03-09-2007 03:10 AM
Dual binary/character streams? Adam Warner Java 18 11-08-2005 02:57 AM
Detecting binary verses text file streams Tron Thomas C++ 3 11-08-2004 10:07 PM



Advertisments