<< < 3 4 5 6 7 8 9 10 11 12 13 > >>   Sort: Rank

I figured I could use scanf more safely if I checked its ...
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", &amp;n) == 1) break; printf("try again: "); } printf("you typed %d\n", n); but sometimes it seems to go...
2015-10-19, 983👍, 0💬

How can I tell how much destination buffer space ...
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? When the format string being used with sprintf is known and relatively simple, you can sometimes predict a buffer size in an ad-hoc way. If the f...
2015-10-19, 1066👍, 0💬

What is the deal on sprintfs return value?
What's the deal on sprintf's return value? Is it an int or a char *? The Standard says that it returns an int (the number of characters written, just like printf and fprintf). Once upon a time, in some C libraries, sprintf returned the char * value of its first argument, pointing to the completed re...
2015-10-16, 892👍, 0💬

Why does everyone say not to use gets?
Why does everyone say not to use gets? Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer if an input line is longer than expected--and Murphy's Law says that, sooner or later, a larger-than-expected input line will ...
2015-10-16, 978👍, 0💬

I thought I would check errno after a long string of printf calls ...
I thought I'd check errno after a long string of printf calls, to see if any of them had failed: errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno)); Why is it printing something strange like ``printf f...
2015-10-14, 954👍, 0💬

What is the difference between fgetpos/fsetpos and ftell/fseek? What are fgetpos and fsetpos good for?
What is the difference between fgetpos/fsetpos and ftell/fseek? What are fgetpos and fsetpos good for? ftell and fseek use type long int to represent offsets (positions) in a file, and may therefore be limited to offsets which can be represented in a long int. (Type long int is not guaranteed to hol...
2015-10-14, 1693👍, 0💬

How can I flush pending input so that a user ...
How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work? fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be ...
2015-10-12, 918👍, 0💬

If fflush wont work, what can I use to flush input?
If fflush wont work, what can I use to flush input? It depends on what you're trying to do. If you're trying to get rid of an unread newline or other unexpected input after calling scanf you really need to rewrite or replace the call to scanf. Alternatively, you can consume the rest of a partially-r...
2015-10-12, 974👍, 0💬

I wrote this routine which is supposed to open a fi
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. Functions in C always receive copies of their arg...
2015-10-09, 866👍, 0💬

I cant even get a simple fopen call to work
I can't even get a simple fopen call to work! What's wrong with this call? FILE *fp = fopen(filename, 'r'); fopen's mode argument must be a string, like "r", not a character like 'r'.
2015-10-09, 914👍, 0💬

How can I open files with names like ...
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.. You want printf's close cousin sprintf, which ``prints'' to a string: char filename[FILENAME_MAX]; sprintf(filename, "file%d", i)...
2015-10-07, 1002👍, 0💬

I am trying to update a file in place ...
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. Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is a...
2015-10-07, 948👍, 0💬

How can I insert or delete a line (or record) in the middle of a file?
How can I insert or delete a line (or record) in the middle of a file? In general, there is no way to do this. The usual solution is simply to rewrite the file. When you find yourself needing to insert data into an existing file, here are a few alternatives you can try: * Rearrange the data file so ...
2015-10-05, 1021👍, 0💬

How can I recover the file name given an open stream?
How can I recover the file name given an open stream? This problem is, in general, insoluble. Under Unix, for instance, a scan of the entire disk (perhaps involving special permissions) would theoretically be required, and would fail if the descriptor were connected to a pipe or referred to a delete...
2015-10-05, 904👍, 0💬

Once I have used freopen, how can I get the original stdout (or stdin) back?
Once I have used freopen, how can I get the original stdout (or stdin) back? There isn't a good way. If you need to switch back, the best solution is not to have used freopen in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leav...
2015-10-01, 1001👍, 0💬

How can I tell if standard input or output is redirected
How can I tell if standard input or output is redirected (i.e. whether ``&lt;'' or ``&gt;'' was used on the invocation command line)? You can't tell directly, but you can usually look at a few other things to make whatever decision you need to. If you want your program to take input from std...
2015-10-01, 904👍, 0💬

How can I arrange to have output go two places at once, e.g. to the screen and to a file?
How can I arrange to have output go two places at once, e.g. to the screen and to a file? You can't do this directly, but you could write your own printf variant which printed everything twice. Here is a sample logprintf function which prints to both stdout and a preopened log file: #include &lt...
2015-09-29, 911👍, 0💬

How can I get back to the interactive keyboard if stdin is redirected?
I'm trying to write a program like ``more.'' How can I get back to the interactive keyboard if stdin is redirected? There is no portable way of doing this. Under Unix, you can open the special file /dev/tty. Under MS-DOS, you can try opening the ``file'' CON, or use routines or BIOS calls such as ge...
2015-09-29, 931👍, 0💬

I want to read and write numbers between files and memory ...
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? What you're trying to do is usually called ``binary'' I/O. First, make sure that you are calling fopen with the "b" modifier ("rb", "wb", e...
2015-09-24, 862👍, 0💬

How can I read a binary data file properly?
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. When you're reading a binary data file, you should specify "rb" mode when calling fopen, to make sure that text file transl...
2015-09-24, 981👍, 0💬

How can I change their mode to binary?
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? There is no standard way to do this. On Unix-like systems, there is no text/binary distinction, so there is no need to change the mode. Some MS-DOS compilers supply a...
2015-09-21, 971👍, 0💬

What is the difference between text and binary I/O?
What is the difference between text and binary I/O? In text mode, a file is assumed to consist of lines of printable characters (perhaps including tabs). The routines in the stdio library (getc, putc, and all the rest) translate between the underlying system's end-of-line representation and the sing...
2015-09-21, 909👍, 0💬

How can I read/write structures from/to data files?
How can I read/write structures from/to data files? It is relatively straightforward to write a structure out using fwrite: fwrite(&amp;somestruct, sizeof somestruct, 1, fp); and a corresponding fread invocation can read it back in. What happens here is that fwrite receives a pointer to the stru...
2015-09-18, 944👍, 0💬

How can I write code to conform to these old, binary data file formats?
How can I write code to conform to these old, binary data file formats? It's hard, because of word size and byte order differences, floating-point formats, and structure padding. To get the control you need over these particulars, you may have to read and write things a byte at a time, shuffling and...
2015-09-18, 926👍, 0💬

<< < 3 4 5 6 7 8 9 10 11 12 13 > >>   Sort: Rank