<< < 11 12 13 14 15 16 17 >   Sort: Date

Why doesnt that code work?
Why doesn't the code short int s; scanf("%d", &s); work? When converting %d, scanf expects a pointer to an int. To convert to a short int, use %hd .
2015-10-28, 1042👍, 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, 1040👍, 0💬

How do I convert a string to all upper or lower case?
How do I convert a string to all upper or lower case? Some libraries have routines strupr and strlwr or strupper and strlower, but these are not Standard or portable. It's a straightforward exercise to write upper/lower-case functions in terms of the toupper and tolower macros in &lt;ctype.h>;(T...
2015-08-19, 1040👍, 0💬

How can I get the current date or time of day in a C program?
How can I get the current date or time of day in a C program? Just use the time, ctime, localtime and/or strftime functions. Here is a simple example: #include &lt;stdio.h> #include &lt;time.h> int main() { time_t now; time(&amp;now); printf("It's %s", ctime(&amp;now)); return 0; } C...
2015-08-07, 1039👍, 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, 1037👍, 0💬

How can I delete a file?
How can I delete a file? The Standard C Library function is remove. (This is therefore one of the few questions in this section for which the answer is not ``It's system-dependent.'') On older, pre-ANSI Unix systems, remove may not exist, in which case you can try unlink.
2015-04-10, 1034👍, 0💬

I need code to parse and evaluate expressions.
I need code to parse and evaluate expressions. Two available packages are ``defunc,'' posted to comp.sources.misc in December, 1993 (V41 i32,33), to alt.sources in January, 1994, and available from sunsite.unc.edu in pub/packages/development/libra ries/defunc-1.3.tar.Z,and ``parse,'' at lamont.ldgo....
2015-05-01, 1033👍, 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, 1028👍, 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, 1024👍, 0💬

Why is p turning out const, instead of the characters pointed to?
I've got the declarations typedef char *charp; const charp p; Why is p turning out const, instead of the characters pointed to? typedef substitutions are not purely textual.In the declaration const charp p; p is const for the same reason that const int i declares i as const. The typedef'ed declarati...
2016-01-04, 1022👍, 0💬

I need a random true/false value ...
I need a random true/false value, so I'm just taking rand() % 2, but it's alternating 0, 1, 0, 1, 0... Poor pseudorandom number generators (such as the ones unfortunately supplied with some systems) are not very random in the low-order bits. (In fact, for a pure linear congruential random number gen...
2015-07-24, 1019👍, 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, 1012👍, 0💬

I know that the library function localtime will convert ...
I know that the library function localtime will convert a time_t into a broken-down struct tm, and that ctime will convert a time_t to a printable string. How can I perform the inverse operations of converting a struct tm or a string into a time_t? ANSI C specifies a library function, mktime, which ...
2015-08-05, 1008👍, 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, 1001👍, 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, 1000👍, 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, 999👍, 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, 993👍, 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", &amp;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...
2015-10-23, 992👍, 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, 990👍, 0💬

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, 985👍, 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, 984👍, 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, 984👍, 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, 982👍, 0💬

I am getting strange syntax errors on the very first declaration in a file, but it looks fine.
I am getting strange syntax errors on the very first declaration in a file, but it looks fine. Perhaps there's a missing semicolon at the end of the last declaration in the last header file you're #including.
2016-02-12, 966👍, 0💬

<< < 11 12 13 14 15 16 17 >   Sort: Date