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

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, 457👍, 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, 542👍, 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, 475👍, 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, 468👍, 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(&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 structur...
2015-09-18, 508👍, 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, 504👍, 0💬

Why isnt it being handled properly?
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? Character sequences like \n are interpreted at compile time. When a backslash and an adjacent n appear in a character constant or string...
2015-08-24, 544👍, 0💬

How can I convert numbers to strings (the opposite of atoi)? Is there an itoa function?
How can I convert numbers to strings (the opposite of atoi)? Is there an itoa function? Just use sprintf: sprintf(string, "%d", number); (Don't worry that sprintf may be overkill, potentially wasting run time or code space; it works well in practice.) You can obviously use sprintf to convert long or...
2015-08-24, 726👍, 0💬

Does C have anything like the `substr (extract substring) routine present in other languages?
Does C have anything like the `substr (extract substring) routine present in other languages? Not as such. To extract a substring of length LEN starting at index POS in a source string, use something like char dest[LEN+1]; strncpy(dest, &source[POS], LEN); dest[LEN] = '\0'; /* ensure \0 terminat...
2015-08-19, 607👍, 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 <ctype.h>;(The o...
2015-08-19, 549👍, 0💬

Why do some versions of toupper act strangely if given an upper-case letter?
Why do some versions of toupper act strangely if given an upper-case letter? Why does some code call islower before toupper? In earlier times, toupper was a function-like preprocessor macro and was defined to work only on lower-case letters; it misbehaved if applied to digits, punctuation, or letter...
2015-08-17, 611👍, 0💬

How can I split up a string into whitespace-separated fields?
How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv? The only Standard function available for this kind of ``tokenizing'' is strtok, although it can be tricky to use and it may not do everything you want it to. (For in...
2015-08-17, 569👍, 0💬

I need some code to do regular expression and wildcard matching.
I need some code to do regular expression and wildcard matching. Make sure you recognize the difference between: * Classic regular expressions, variants of which are used in such Unix utilities as ed and grep. In regular expressions, a dot . usually matches any single character, and the sequence .* ...
2015-08-12, 624👍, 0💬

I am trying to sort an array of strings with qsort, using strcmp as the comparison function, but it is not working.
I am trying to sort an array of strings with qsort, using strcmp as the comparison function, but it is not working. By ``array of strings'' you probably mean ``array of pointers to char.'' The arguments to qsort's comparison function are pointers to the objects being sorted, in this case, pointers t...
2015-08-12, 617👍, 0💬

Now I am trying to sort an array of structures with qsort ...
Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning? The conversions must be in the comparison function,...
2015-08-10, 626👍, 0💬

How can I sort a linked list?
How can I sort a linked list? Sometimes it's easier to keep the list in order as you build it (or perhaps to use a tree instead). Algorithms like insertion sort and merge sort lend themselves ideally to use with linked lists. If you want to use a standard library function, you can allocate a tempora...
2015-08-10, 590👍, 0💬

How can I sort more data than will fit in memory?
How can I sort more data than will fit in memory? You want an ``external sort,'' which you can read about in Knuth, Volume 3. The basic idea is to sort the data in chunks (as much as will fit in memory at one time), write each sorted chunk to a temporary file, and then merge the files. Your operatin...
2015-08-07, 555👍, 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 <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("It's %s", ctime(&now)); return 0; } Calls to localtim...
2015-08-07, 538👍, 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, 520👍, 0💬

How can I add N days to a date? How can I find the difference between two dates?
How can I add N days to a date? How can I find the difference between two dates? The ANSI/ISO Standard C mktime and difftime functions provide some (limited) support for both problems. mktime accepts non-normalized dates, so it is straightforward to take a filled-in struct tm, add or subtract from t...
2015-08-05, 578👍, 0💬

Did C have any Year 2000 problems?
Did C have any Year 2000 problems? No, although poorly-written C programs might have. The tm_year field of struct tm holds the value of the year minus 1900; this field therefore contains the value 100 for the year 2000. Code that uses tm_year correctly (by adding or subtracting 1900 when converting ...
2015-08-03, 587👍, 0💬

I need a random number generator.
I need a random number generator. The Standard C library has one: rand. The implementation on your system may not be perfect, but writing a better one isn't necessarily easy, either. Here is a portable C implementation of the ``minimal standard'' generator : #define a 16807 #define m 2147483647 #def...
2015-08-03, 884👍, 0💬

How can I get random integers in a certain range?
How can I get random integers in a certain range? The obvious way, rand() % N /* POOR */ (which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. A better method is something like (int)((double)rand() / ((double)...
2015-07-29, 561👍, 0💬

Each time I run my program, I get the same sequence of numbers back from rand
Each time I run my program, I get the same sequence of numbers back from rand It's a characteristic of most pseudo-random number generators (and a defined property of the C library rand) that they always start with the same number and go through the same sequence. (Among other things, a bit of predi...
2015-07-29, 616👍, 0💬

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