Thread: Reading a mpeg video file

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    4

    Unhappy Reading an mpeg video file

    i have been working with vidoe files and my task is open a video file, get it copied into another file and then changing into binay format. after getting few bits inverted, changing back it into ascii formats. original video contains normal and extended ascii codes. unfortunately, i got stuck at first step, whole video file is not being copied. i have wirtten a piece of code that copies the whole word/ any text file but the video file is not fully copied as just 5KB out of 119,659 KB are copied. plz see the code and help me out

    Code:
    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    
    FILE *fp1,*fp2;
        
        char c;
    
    
        fp1 = fopen( "sample.mpg", "r") ;
        fp2 = fopen("result.txt","w");
        c = getc(fp1) ;    
            while ( c != EOF) 
            { 
                putc( c,  fp2);
                c =  getc( fp1 ) ;
            } 
    
    
    fclose ( fp1 );
    fclose(fp2);
    }
    Last edited by Afzaal Ahmed; 10-28-2013 at 02:10 AM.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What is the return type of getc()?

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    it returns the characters it read, but fails after reading about 1/4 of a video file

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Afzaal Ahmed View Post
    it returns the characters it read, but fails after reading about 1/4 of a video file
    it returns int and you store it into char
    so when char value 0xFF is read and compared to EOF (-1) - your loop is broken.
    make c an int.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    i have mistakenly declared c as a character here, in the original code c is actually an integer. But this problem persists for c as an int and it is only for the video files while any text file is fully copied.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    change the mode to rb and wb correspondingly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Afzaal Ahmed View Post
    i have mistakenly declared c as a character here, in the original code c is actually an integer. But this problem persists for c as an int and it is only for the video files while any text file is fully copied.
    You need to open your files in binary mode, ie: "rb" and "wb" as opposed to "r" and "w".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    4
    thanks to all of you for sparing your time, replacing r and w with 'rb' and 'wb' has solved the issue.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > #include "stdafx.h"
    > int _tmain(int argc, _TCHAR* argv[])
    Ah, windows user.

    In which case, you need to use "rb" and "wb" as your file open modes to force the file to be read as binary data.

    If you read binary data in text mode, the first 0x1A byte it sees signals EOF.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Salem View Post
    If you read binary data in text mode, the first 0x1A byte it sees signals EOF.
    And 0x0D 0x0A (aka, '\r' '\n') combinations will be changed to 0x0A.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading raw avi video data
    By dawatson in forum Tech Board
    Replies: 8
    Last Post: 10-11-2011, 03:36 AM
  2. AVI to MPEG software?
    By cpjust in forum General Discussions
    Replies: 8
    Last Post: 03-15-2010, 06:19 AM
  3. MPEG 4 decoder
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 05-10-2007, 11:32 PM
  4. MPEG player
    By The Stupid One in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2002, 05:45 AM
  5. .wav to .mpeg?
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-21-2002, 01:16 AM

Tags for this Thread