I have to parse different fields of PAT table in this file...
the file looks like this in hex editor...
47 40 00 10 00 00 B0 0D 00 00 C3 00 00 00 01 E0 21 63 0D 38 1E ......
.......
my code is

/*parse1.c*/
    [B]
    #include <stdio.h>
    #include <string.h>
    void main() {

    FILE *f;
    int num[50];
    memset(num,0,50);
    short unsigned int i=0;
    short int rv;
    //short unsigned int num_values;
    f=fopen("screen.ts","r");
    //num[0]='0';
     if (f==0){
        printf("file doesnt exist?!\n");
     }
     while (i<50) {

        rv = fscanf(f, "%d", &num[i]);

        //if (rv != 1)

          //break;        
    i++;
    }
    printf("table id = %d%d \n",num[10],num[11]);
    int section_syntax_indicator=num[12] & 0x0008;
printf("section_syntax_indicator=%d\n",section_syntax_indicator);
    int secl = num[13] & 0x0001;
printf("section length= %d%d%d \n",secl,num[14],num[15]); printf("transportstreamid=%d%d%d%d",num[16],num[17],num[18],num[19]);
    int var1=num[20] & 0x0003;         
    int var2=num[21] & 0x000E;
    int currentnext=num[21] & 0x0001;    
    printf("version no=%d%d \n",var1,var2);
    printf("current_next=%d \n",currentnext);
    printf("section no=%d%d \n",num[22],num[23]);
    printf("last section number=%d%d \n",num[24],num[25]);
   printf("programnumber=%d%d%d%d\n",num[26],num[27],num[28],num[29]);
    int var3=num[30] & 0x0001;    printf("programmapid=%d%d%d%d\n",var3,num[31],num[32],num[33]); 

    if(fclose(f)==0)
     {
    printf("closed");
    }
    }[/B]

the output im getting is:

table id = 00
section_syntax_indicator= 0
section length= 036625504
transport stream id =0012190version no=00
current_next=0
section no=1345133176688432
last section number=1345131321
program number=6623172-10792550886625944-1079255132
program map pid=0-1079255148134513132-1079255160
closed

can anyone suggest me where the problem is?.thanks in advance.

Dani AI

Generated

Quick diagnosis: the posted symptoms come from reading a binary MPEG‑TS file as text and from incorrect sizes/types. was right to flag the fopen NULL check and the "%x" idea, but the real fixes are: (1) open the file in binary mode, (2) read raw bytes with fread/fgetc (not fscanf), (3) store bytes in unsigned 8‑bit types, and (4) zero buffers with the correct byte count. The garbage output and huge numbers are caused by wrong masking after sign extension, by calling memset with the wrong length, and by printing multi‑byte fields with chained "%d%d" (which just concatenates decimal output).

Concrete checklist and rules to follow:

  • Use fopen("screen.ts","rb") and check if (!f) { perror(...); return; }.

  • Read bytes with size_t n = fread(buf,1,sizeof buf,f); and verify n.

  • Use uint8_t (or unsigned char) for raw bytes so bitmasks behave predictably.

  • Zero memory with memset(buf,0,sizeof buf), not a fixed byte count of 50.

  • When assembling multi‑byte fields use shifts and ORs, and mask bits explicitly. For example, assuming sec points to the start of a PSI section:

    uint8_t table_id = sec[0];
    int section_syntax_indicator = (sec[1] >> 7) & 1;
    int section_length = ((sec[1] & 0x0F) << 8) | sec[2];
    uint16_t transport_stream_id = (sec[3] << 8) | sec[4];
  • Print bytes/fields with clear formatting (use %02X for bytes, %u for unsigned integers) instead of concatenated %d%d%d.

Quick debugging tips: confirm TS alignment by checking for 0x47 every 188 bytes, and if payload_unit_start_indicator is set, use the pointer_field to find the section start. If parsing becomes more than a quick hack, use a tested MPEG‑TS/PSI library (FFmpeg/libavformat or a small TS parser) rather than ad hoc fscanf-based parsing. Applying these changes will remove the garbage values shown in the original output from .

Recommended Answers

All 5 Replies

if (f==0){
        printf("file doesnt exist?!\n");
     }

f is a pointer. Your if condition should check whether it is null. And if so print the message and exit.

Also since you are reading a file that contains hexadecimal numbers , it will contain characters from A to F and in fscanf you are reading integers. use %x as format specifier and see if it works

tried both ,but it is not working,giving wrong values...

Just to confirm, you modified the format specifiers in printf as well, right?

Just to confirm, you modified the format specifiers in printf as well, right?

yup

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.