Thread: fread() value not changing?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    fread() value not changing?

    I'm having problems with the below code - basically, the value of "sample" which is read never seems to change, even though it does in the input file. Any ideas?

    Code:
    inhandle = fileno(infile);
    		filesize = filelength(inhandle);
    
    		for (j = 0; j <= (filesize/2); j++)
    		{
    			fread(&sample, sizeof(short), 1, infile); // Read first sample
    			code = ADPCMEncoder(sample, prevsample, previndex);	// Encode sample into lower nibble of code
    			code = (code << 4) & 0xf0;	// Move ADPCM code to upper nibble
    			
    			// If odd number of samples, do not wait for second nibble of last byte
    			if((j == (filesize/2)) && ((filesize/2)%2 != 0))	
    			{
    				fwrite(&code, sizeof(unsigned char), 1, tempout);
    				break;
    			}
    			
    			fread(&sample, sizeof(short), 1, infile); // Read second sample
    			code = ADPCMEncoder(sample, prevsample, previndex);	// Encode sample and save in lower nibble of code
    			fwrite(&code, sizeof(unsigned char), 1, tempout);	// Write code to file
    		}

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    [QUOTE=subtled;635734]
    Code:
    for (j = 0; j <= (filesize/2); j++)
    This reads one iteration too far, if the file size is even.

    Code:
    fread(&sample, sizeof(short), 1, infile); // Read second sample
    code = ADPCMEncoder(sample, prevsample, previndex);	// Encode sample and save in lower nibble of code
    That blows away the upper nybble of code which you just read. Shouldn't it be:

    Code:
    code |= ADPCMEncoder(sample, prevsample, previndex) & 0x0f;
    Other than that, I don't see problems. Although I don't see any code that updates prevsample or previndex -- not sure what they're for.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Thanks for the reply, prevsample and previndex are updated by another function elsewhere. Have changed "code =" to "code |=", but not sure exactly how it works, could you explain? Also, I've found that there's a problem with the file I was trying to read in, other files work fine.

    What's confusing me though is this: the file I'm trying to read in is an audio file, and it plays fine, if I open it in Notepad there's loads of random non-zero stuff there, yet when I write a simple program to just display it as integers, the first few are ok, then the rest are 0. It's not a problem with my code as other files display fine... Any idea if my code could be getting screwed up by a weird character in the file making it do something funny or anything? It's just a simple read, it's not trying to do any processing on it, which is what I find strange...

    Code:
    #include <stdio.h>
    int main(void)
    {
    FILE * file;
    unsigned char raw;
    
    int i;
    file = fopen("1.pcm","r");
    for(i = 0; i<200; i++)
    {
    fscanf(file,"%c",&raw);
    
    printf("%u ",raw);
    }
    fclose(file);
    }
    And the output is:

    Code:
    46 255 101 255 113 255 168 255 221 255 6 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    (Excuse the sloppy formatting, just put some carriage returns in manually to stop it making the page stupidly wide!)

    The file is here if you want to look at it.

    Any help appreciated!

    Andy

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Firstly, you assume that its in ASCII, as in each character is 1 byte wide. I'd use fgetc() rather than fscanf() cause its faster.

    Also, Would indenting your code really kill you?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Apologies, I normally indent code, but this was a quick and dirty program written just to check something so I didn't bother. Point taken though

    Changed it to fgetc and I get the same result but with all the zeros replaced by 255.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so I didn't bother.
    People reading posts as well take the same approach in deciding whether to help or not.

    Crap posts take 2nd place to nicely indented posts with well presented information and a clear and specific question.
    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.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by subtled View Post
    Any idea if my code could be getting screwed up by a weird character in the file making it do something funny or anything? It's just a simple read, it's not trying to do any processing on it, which is what I find strange...
    More than likely. You aren't opening the file in binary mode. So fread() may be corrupting your data in some way, perhaps causing a premature EOF. Open in "rb" mode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM