CodeSOD: The Sound of GOTO

This post was originally published on this site

The Daily WTF

Let’s say you have an audio file, or at least, something you suspect is an audio file. You want to parse through the file, checking the headers and importing the data. If the file is invalid, though, you want to raise an error. Let’s further say that you’re using a language like C++, which has structured exception handling.

Now, pretend you don’t know how to actually use structured exception handling. How do you handle errors?

Adam‘s co-worker has a solution.

char id[5]; // four bytes to hold ‘RIFF’ bool ok = false; id[sizeof(id) – 1] = 0; do { size_t nread = fread(id, 4, 1, m_sndFile); // read in first four bytes if (nread != 1) { break; } if (strcmp(id, “RIFF”)) { break; } // … // 108 more lines of file parsing code like this // … ok = true; } while (time(0L) == 0); // later if

To read the full article click on the 'post' link at the top.