Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Files

Status
Not open for further replies.

ASingerMustDie

Programmer
Feb 1, 2001
17
GB
Hi All :)

Sorry to bother you with something surely so trivial...

I am attempting to read the first 4 bytes of a file and ensuring that it reads "RIFF" (perhaps for obvious reasons).

I am using ReadFile to achieve this, the problem being that the buffer I read into seems to have an extra (null?) character appended to it, or so it appears when I attempt to debug. I know that character arrays are terminated by a null character, but if this is what is happening, how do I then compare the buffer to the string "RIFF"?

The code I have is (chopped for ease of reading)...

char waveBuffer[4];
DWORD waveRead;
HANDLE waveHandle;

//Assume file is opened

ReadFile(waveHandle, &waveBuffer, 4, &waveRead, NULL);
if (!strcmp(waveBuffer, "RIFF"))
{
//Is Good;
}
else
{
//Is not good
}

I cant manage to have it work for files that DO begin with the four characters RIFF. Why is this?

Any help would be most appreciated.

ASMD

PS And why-oh-why when I opt to click the Help menu am I told that a required component is missing and that I must reinstall?
 
Before using strcmp(..) the buffer should be copied to a exact string. ReadFile gives the buffer and no. of chracters read in " waveRead".SO don't use buffer to string compare directly.
 
OK...all of a sudden I feel like I need my hand held...
I tried to copy the buffer thus:

char waveHead[4];

//Open and read file

strcpy(waveHead, waveBuffer);

But this not only crashed the program but the entire computer...it has been so long since I dabbled in C++.

Thanks for the help so far and any subsequent aid.
ASMD
 


ok, Just add this line after ReadFile(...)

waveBuffer[waveRead]='\0';

if (!strcmp(waveBuffer, "RIFF"))
{
.............
}
 
Why not try like this:
strncmp(waveBuffr,"RIFF",4); ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top