Posts Tagged ‘programming’

How to convince any C developer to dump gcc and use clang

Posted in c, programming on March 6th, 2010 by fseek – 20 Comments

Most C developers I know hate those wrong or incomplete warnings that GCC sometimes give to us. Plus, it seems to always happen when you are in a rush trying to debug something or get the code out.

However today, I saw a screen shot that convinced me to give clang a shot (at least on my test environments). For those who doesn’t know, clang is C/C++ front-end for the LLVM compiler.

Take a look and let me know if you can find one C developer who is not convinced to use clang :)

Clang

Don’t forget to fflush, don’t forget to fflush

Posted in c, tips on February 26th, 2010 by fseek – Be the first to comment

This is more like a self reminder, since I spent hours fighting with such a simple issue…

Yesterday I was writing some C code that had to add some content to a file, pass that file to another program, then modify this same file a bit more and save it. Something very simple, like:

nstruc->fp = fopen(nstruc->file, "w+");
if(!nstruc->fp)
{
 p_error("Unable to open %s", nstruc->file);
 return(0);
}
write2fp(nstruc->fp);
send_file_to_externalprocess(nstruc->file);
writemore2fp(nstruc->fp);
fclose(nstruc->fp);
The problem is that when sending that file to the external process (exec), the file was always empty. “What is going on??”, I thought… After adding a bunch of debug statements, cursing a bit, I finally remembered:

I FORGOT TO FFLUSH!

For those not familiar with C, when you write something to stdout (or any file descriptor), the output is not written immediately, but it is buffered internally and only written when the internal buffer is full, the file is closed or you call fflush. So, the next time you can’t find out why a file is not being written, check if you called fflush. The fixed code looked like this:

nstruc->fp = fopen(nstruc->file, "w+");
if(!nstruc->fp)
{
 p_error("Unable to open %s", nstruc->file);
 return(0);
}
write2fp(nstruc->fp);
fflush(nstruc->fp);
send_file_to_externalprocess(nstruc->file);
..

int fseek(FILE *stream, long offset, int whence);

Posted in c, fseek, programming on February 8th, 2010 by fseek – Be the first to comment

For a site named fseek, the first post had to be about the fseek C function.

The fseek() function sets the file position indicator for the stream
pointed to by stream. The new position, measured in bytes, is obtained
by adding offset bytes to the position specified by whence. If whence is
set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the
start of the file, the current position indicator, or end-of-file, re-
spectively. A successful call to the fseek() function clears the end-of-
file indicator for the stream and undoes any effects of the ungetc(3)
function on the same stream.

Example: How to use fseek to set the file position to the end of a file

FILE *fp;
fp = fopen(“myfile.txt”, “r”);
if(fp)
{
fseek(fp, 0, SEEK_SET);
}

Simple and easy. About this blog? Expect a lot about C, Unix, Linux, programming and hacking in general.