top/contents search

12. Stdio

12.1 What's wrong with this code?

char c;
while((c = getchar()) != EOF) ...

12.1b I have a simple little program that reads characters until EOF, but how do I actually enter that ``EOF'' value from the keyboard? I see that EOF is defined by <stdio.h> to be -1; am I supposed to enter -1?

12.2 Why does the simple line-copying loop while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } copy the last line twice?

12.3 I'm using fgets to read lines from a file into an array of pointers. Why do all the lines end up containing copies of the last line?

12.4 My program's prompts and intermediate output don't always show up on the screen, especially when I pipe the output through another program.

12.5 How can I read one character at a time, without waiting for the RETURN key?

12.6 How can I print a '%' character in a printf format string? I tried \%, but it didn't work.

12.7 Why doesn't

long int n = 123456;
printf("%d\n", n);

12.8 I thought that ANSI function prototypes were supposed to guard against argument type mismatches.

12.9 Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf?

12.9b What printf format should I use for a typedef like size_t when I don't know whether it's long or some other type?

12.10 How can I implement a variable field width with printf? That is, instead of something like %8d, I want the width to be specified at run time.

12.11 How can I print numbers with commas separating the thousands?
What about currency formatted numbers?

12.12 Why doesn't the call scanf("%d", i) work?

12.12b Why does the call

char s[30];
scanf("%s", s);
work? I thought you always needed an & on each variable passed to scanf.

12.13 Why doesn't this code:

double d;
scanf("%f", &d);

12.14 Why doesn't the code

short int s;
scanf("%d", &s);

12.15 How can I specify a variable width in a scanf format string?

12.16 How can I read data from data files with particular formats?
How can I read ten floats without having to use a jawbreaker scanf format
like "%f %f %f %f %f %f %f %f %f %f"?
How can I read an arbitrary number of fields from a line into an array?

12.17 When I read numbers from the keyboard with scanf and a "%d\n" format, like this:

	int n;
	scanf("%d\n", &n);
	printf("you typed %d\n", n);
it seems to hang until I type one extra line of input.

12.18a I'm reading a number with scanf and %d, and then a string with gets():

	int n;
	char str[80];

	printf("enter a number: ");
	scanf("%d", &n);
	printf("enter a string: ");
	printf("you typed %d and \"%s\"\n", n, str);
but the compiler seems to be skipping the call to gets()!

12.18b I'm using scanf %c to read a Y/N response, but later input gets skipped.

12.19 I figured I could use scanf more safely if I checked its return value to make sure that the user typed the numeric values I expect:

	int n;

	while(1) {
		printf("enter a number: ");
		if(scanf("%d", &n) == 1)
		printf("try again: ");

	printf("you typed %d\n", n);
but sometimes it seems to go into an infinite loop. [footnote] Why?

12.20 Why does everyone say not to use scanf? What should I use instead?

12.21 How can I tell how much destination buffer space I'll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf?

12.22 What's the deal on sprintf's return value? Is it an int or a char *?

12.23 Why does everyone say not to use gets()?

12.24 I thought I'd check errno after a long string of printf calls, to see if any of them had failed:

	errno = 0;
	if(errno != 0)
		fprintf(stderr, "printf failed: %s\n", strerror(errno));
Why is it printing something strange like ``printf failed: Not a typewriter'' when I redirect the output to a file?

12.25 What's the difference between fgetpos/fsetpos and ftell/fseek?
What are fgetpos and fsetpos good for?

12.26a How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?

12.26b If fflush won't work, what can I use to flush input?

12.27 I wrote this routine which is supposed to open a file:

	myfopen(char *filename, FILE *fp)
		fp = fopen(filename, "r");
But when I call it like this:
		FILE *infp;
		myfopen("filename.dat", infp);
the infp variable in the caller doesn't get set properly.

12.28 I can't even get a simple fopen call to work! What's wrong with this call?

	FILE *fp = fopen(filename, 'r');

12.28b How can I open files with names like ``file1'', ``file2'', ``file3'', etc., where the numeric part is controlled by a variable? Basically I want ``file%d'', like printf.

12.29 fopen is failing for certain pathnames.

12.30 I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working.

12.31 How can I insert or delete a line (or record) in the middle of a file?

12.32 How can I recover the file name given an open stream?

12.33 How can I redirect stdin or stdout to a file from within a program?

12.34 Once I've used freopen, how can I get the original stdout (or stdin) back?

12.35 How can I tell if standard input or output is redirected (i.e. whether ``<'' or ``>'' was used on the invocation command line)?

12.36 I'm trying to write a program like ``more.'' How can I get back to the interactive keyboard if stdin is redirected?

12.36b How can I arrange to have output go two places at once, e.g. to the screen and to a file?

12.37 I want to read and write numbers between files and memory in a byte-at-a-time way, not as formatted characters the way fprintf and fscanf do. How can I do this?

12.38 How can I read a binary data file properly? I'm occasionally seeing 0x0a and 0x0d values getting garbled, and I seem to hit EOF prematurely if the data contains the value 0x1a.

12.39 I'm writing a ``filter'' for binary files, but stdin and stdout are preopened as text streams. How can I change their mode to binary?

12.40 What's the difference between text and binary I/O?

12.41 How can I read/write structures from/to data files?

12.42 How can I write code to conform to these old, binary data file formats?

12.43 I'm reading strings typed by the user into an array, and then printing them out later. When the user types a sequence like \n, why isn't it being handled properly?


contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North