Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion rtf file verification
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tor Rustad  
View profile  
 More options Jun 11 2009, 7:37 pm
Newsgroups: comp.lang.c
From: Tor Rustad <bwz...@wvtqvm.vw>
Date: Fri, 12 Jun 2009 01:37:10 +0200
Local: Thurs, Jun 11 2009 7:37 pm
Subject: Re: rtf file verification

Cross wrote:
> Hello

> I am writing a program to verify if it is an rtf file or not. Now, an rtf file
> starts with {\rtf<N>.

> My code is as follows:
> int main(int argc, char **argv){
>    char str[5], c;
>    FILE *fstream

>    //code for connecting to file stream

>    c=getc(fstream);

return type of getc() is int, not char. Need to check for EOF.

>    if(c!='{'){
>     // An rtf file should start with `{\rtf'
>    fprintf(stderr, "invalid rtf file\n");
>    }else if(fscanf(fstream, "%4s", &str), strcmp(str, "\rtf")!=0 ){

'&str' is wrong, could use '&str[0]' or 'str'.

using comma operator here is terrible for readability and wrong, in case
of read error you invoke UB.

>    fprintf(stderr, "rtf version unspecified.\n");
>     // check: if str prints "\rtf", why strcmp returns non-zero value?

others have answered that...

>    printf("%s\n", str);
>    }else{
>    // stuff to do
>    }
>    return 0;
> }

> I tried to check the code against an rtf file. The "\rtf" tag check fails

/**
  * check if file has prefix "{\rtf"
  */
int is_rtf(FILE *f)
{
         char            buf[6]={0};
         const char      *prefix = "{\\rtf";

         if (NULL == fgets(buf, sizeof buf, f)) {
                 perror("is_rtf()");
                 exit(EXIT_FAILURE);
         }

         return 0==strcmp(prefix, buf);

}

--
Tor <echo bwz...@wvtqvm.vw | tr i-za-h a-z>

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.