Thread: string to file? help?

  1. #1
    cablet
    Guest

    string to file? help?

    Greetings,
    I have created a unix program that converts a targa file to a YUV video file. I used unsigned char*'s to handle the file data. I can read large files into my char*'s but am unable to put those large char*'s back into a file. If the file is small (80K) there is no problem writing to file. But when I try 1.1 megs (which is my goal) the new file size is 1 byte (the first byte of the char pointer).

    Question:
    How can I write a large unsigned char* to a newly created file? I can currently read in large files but not write to a new one. I have tried setbuf() as well as fputc() in a loop but either way my file size is 1. (unless the original data is less then about 133kb).

    here is a sample of what I am trying. yuv is my unsigned char *
    please presume that memory and variables have been correctly allocated or declared

    /****** code sample ********/

    unsigned char* tempPtr;
    if( (Nfp = fopen(newfile, "w" )) == NULL)
    {
    printf("Problem creating new file!\n");
    exit(1);
    }

    fseek(Nfp,0,SEEK_SET);
    for(x=0;x<w*h*2;x++); //w*h*2 is the length of yuv
    {
    tempPtr = yuv + x;
    fputc(*tempPtr,Nfp);
    } */

    close(Nfp);


    any ideas would greatly help
    thanks
    cablet

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, you have a semicolen at the end of the for loop. That may be a typo, though. Anyway, I would personally use fwrite since this means just one call:

    fwrite(yuv, sizeof(unsigned char), w*x*2, Nfp);

    Also, you can just use the pointer...

    Code:
    tempPtr = yuv;
    
    for( x = 0; x < (w*h*2); x++ )  {
     fputc(*tempPtr,Nfp);  
     tempPtr++;
     //...fputc(*(tempPtr++), Nfp);  
     }
    Other than that, it looks OK. Post more code if the problem persists...
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM