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

How can I read data from data files with particular formats?
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? In general, there are three main ways of parsing data l...
2015-10-26, 462👍, 0💬

When I read numbers from the keyboard with scanf ...
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. Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to read a...
2015-10-23, 454👍, 0💬

I am reading a number with scanf and d, and then a string with gets
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: "); gets(str); printf("you typed %d and "%s"\n", n, str); but the compiler seems to be skipping the call to gets()! If, in response to ...
2015-10-23, 445👍, 0💬

Why does everyone say not to use scanf? What should I use instead?
Why does everyone say not to use scanf? What should I use instead? scanf has a number of problems, its %s format has the same problem that gets() has --it's hard to guarantee that the receiving buffer won't overflow. More generally, scanf is designed for relatively structured, formatted input (its n...
2015-10-21, 462👍, 0💬

I am using scanf c to read a Y/N response
I'm using scanf %c to read a Y/N response, but later input gets skipped. You wanted scanf %c to read a single character, and it tried to, but when you tried to type that single character at it, before the rest of the input system would accept it, you had to hit the RETURN key, too. scanf read only t...
2015-10-21, 462👍, 0💬

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", &n) == 1) break; printf("try again: "); } printf("you typed %d\n", n); but sometimes it seems to go int...
2015-10-19, 460👍, 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, 494👍, 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, 450👍, 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, 449👍, 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, 455👍, 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, 438👍, 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, 456👍, 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, 442👍, 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, 448👍, 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, 461👍, 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, 490👍, 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, 478👍, 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, 487👍, 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, 441👍, 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, 463👍, 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 ``<'' or ``>'' 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 stdin when ...
2015-10-01, 467👍, 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 <std...
2015-09-29, 508👍, 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, 454👍, 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, 450👍, 0💬

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