Talk:C file input/output

From Wikipedia, the free encyclopedia

< Talk:C file input
This article is supported by the Wikipedia:WikiProject C++. Please visit the project page for more details on the project.

[edit] Faulty example

Removed from the end of the article:

A convenient, but non-standard, way to find the length of a file in C is:

FILE *f = fopen("filename", "rb"); fseek(f, 0L, SEEK_END); length = tell(f); rewind(f);

There are several problems with this code; first of all, tell is not a standard C library function. Replacing it with ftell, the code will compile (assuming length is declared long int somewhere), but it still won't work everywhere; fseek is not guaranteed to produce meaningful results on binary files with SEEK_END. And then there's one minor nitpick: the programmer takes care to rewind the file to the beginning when he's "done" with it, but he never closes the file! That's not a bug, but it may well be a mistake.

[edit] Merge fopen and fclose

It's like yin and yang ... it's awkward to discuss one without the other, and certainly there is repetition in each fopen and fclose. However, a better question is what should the merged article be named? I suggest:

Please suggest! (Or argue if these two files should be not be merged). +mwtoews 00:05, 29 January 2007 (UTC)

Okay, merging them to C file input/output, using the content-base from fopen. +mwtoews 08:55, 1 February 2007 (UTC)