This is the talk page for discussing improvements to the C file input/output article. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google (books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||
|
Removed from the end of the article:
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.
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)
fopen
. +mwtoews 08:55, 1 February 2007 (UTC)I'd like to see all the file I/O functions merged into this article — fread, fwrite, fgetc, fflush, getchar, putchar. They're all permanent stubs, since there's nothing interesting to say about them, and Wikipedia is not a how-to guide to C programming, nor is Wikipedia a man page. (Nobody should be coming to Wikipedia to read a man page, because of the potential for vandalism. There are authoritative, non-modifiable HTML man pages available elsewhere on the Web.)
printf and gets definitely deserve their own articles. rewind, remove, and tmpfile are obscure enough (and substub enough) that I wouldn't want to bring them into an article on file I/O unnecessarily; I'm prodding them now.
Finally, I see no reason this article should be at C file input/output instead of the shorter and universally recognized term C file I/O. It looks like there's been some ambivalence about that term on Wikipedia in the past, judging from the inconsistency between Category:Input/Output and the article Input/output. --Quuxplusone 03:47, 31 March 2007 (UTC)
On systems where int and char are the same size, even the "good" example will suffer from the indistinguishability of EOF and some character's value. The proper way to handle this situation is to check feof and ferror after getchar returns EOF. If feof indicates that end-of-file has not been reached, and ferror indicates that no errors have occurred, then the EOF returned by getchar can be assumed to represent an actual character. These extra checks are rarely done, because most programmers assume that their code will never need to run on one of these "big char" systems.
The EOF Pitfall "Mistaken" code and "Corrected" code are char-for-char identical. Am I missing something here? --DragoonWraith (talk) 05:03, 16 August 2010 (UTC)
So as noted in the carriage return page here in Wikipedia, Windows interprets a 'new line character' '\n' as being a carriage-return '\r' followed by the newline. I am currently working on a project where I output binary files and for a second I thought I was going crazy when I noticed I had extra bits all over the place. It turns out that using fputc, or any other such function with an output character value of 0x0A on a Windows O.S. will cause the output of 0x0D right before it. I.E. if I do the following fputc('\n', fp); or fputc(10, fp); close the file, and examine it in a hex editor, there will be two characters written, '\r' and '\n'. Does anyone know a way around this? 76.26.238.56 (talk) 16:59, 21 November 2010 (UTC)
The following pages essentially discuss the same topic of C input/output functions: stdio.h, putc, putchar, fgets, fgetws, fread, fseek, ferror, gets, setvbuf, feof, fgetpos, ungetc. I propose to cleanup these pages by removing the material that fails WP:NOTMANUAL and by merging the remains into C file input/output.1exec1 (talk) 23:17, 8 October 2011 (UTC)
There is a move discussion in progress which affects this page. Please participate at Talk:C standard library - Requested move and not in this talk page section. Thank you. —RM bot 09:40, 8 November 2011 (UTC)
printk redirects to this page; but this (Linux-specific?) function is a private kernel call (i.e. intended to be called only from kernel mode); it should not be considered as a classic C file I/O. Teuxe (talk) 13:41, 24 July 2012 (UTC)
Should a section be made that gives lip-service to the old UNIX-like "unbuffered" I/O functions? These are read(), write(), open(), creat(), close(), lseek(), and unlink(). Jason Quinn (talk) 04:25, 16 December 2012 (UTC)
Using '<stdio.h>' instead of 'stdio.h' in every reference to to a header file is just stupid, redundant and wrong. The file is called "stdio.h". The brackets are just part of the C syntax, not the name of the file, and are not even specific to standard system headers.
The uniform use of angle brackets should be completely removed from all of the C header file pages, except for the rare cases where they're actually correct/relevant (e.g. source code examples). — Preceding unsigned comment added by 82.9.176.129 (talk) 16:58, 1 September 2014 (UTC)
I would like to match the curly braces on a single style and before, after the if insert a blank line and omit the dispensable void in parameter list, so you can read the code better
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buffer[5] = {0}; /* Initialized to zeroes */
int i;
FILE *fp = fopen("myfile", "rb");
if (fp == NULL) {
perror("Failed to open file \"myfile\"");
return EXIT_FAILURE;
}
for (i = 0; i < 5; i++) {
int rc = getc(fp);
if (rc == EOF) {
fputs("An error occurred while reading the file.\n", stderr);
return EXIT_FAILURE;
}
buffer[i] = rc;
}
fclose(fp);
printf("The bytes read were... %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
return EXIT_SUCCESS;
}
--Diaspomod (talk) 00:41, 21 August 2019 (UTC)
This article was the subject of an educational assignment supported by Wikipedia Ambassadors through the India Education Program.
The above message was substituted from ((IEP assignment))
by PrimeBOT (talk) on 19:58, 1 February 2023 (UTC)
The redirect Gets s has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at Wikipedia:Redirects for discussion/Log/2023 April 26 § Gets s until a consensus is reached. Steel1943 (talk) 04:34, 26 April 2023 (UTC)
The redirect Vasprintf has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at Wikipedia:Redirects for discussion/Log/2023 April 26 § Vasprintf until a consensus is reached. Steel1943 (talk) 04:46, 26 April 2023 (UTC)
The redirect Fputchar has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at Wikipedia:Redirects for discussion/Log/2023 April 26 § Fputchar until a consensus is reached. Steel1943 (talk) 05:21, 26 April 2023 (UTC)
The redirect FILE pointer has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at Wikipedia:Redirects for discussion/Log/2023 April 27 § FILE pointer until a consensus is reached. Steel1943 (talk) 23:59, 27 April 2023 (UTC)
The redirect Sfio has been listed at redirects for discussion to determine whether its use and function meets the redirect guidelines. Readers of this page are welcome to comment on this redirect at Wikipedia:Redirects for discussion/Log/2024 January 19 § Sfio until a consensus is reached. Shhhnotsoloud (talk) 16:55, 19 January 2024 (UTC)