Monday, December 28, 2009

C stuff

How to add non-printable characters to a string

 strcat(test2,"\x01");  01 -- is non-printable ascii character.

 

 

I will be updating the new stuff which come across in C language here, Hope it will be useful. 1. is the header file contains bool datatypes. 2. To read strings from the keyboard we can use two functions: fgets() -- reads a line from fd specified, it will also '\n' which u pass after giving the string so u need to over write it with '\0' . getchar() -- reads character by character. scanf() -- can be used but "white space" is the delimiter in the scanf, if the string has a "white space" in it the string will be treated as two strings. 3. strchr(, ) will retun the address of the character in the string if it is, or else it will return null. 4. every running program will associated with three standard files -- stdout, stderr,stdin. fflush() - Input/Output is nearly 1000 times slower than your processor. Thus it makes no sense for your code to stop executing while the system is writing data somewhere. (you have no choice but to wait if it is reading data - after all you need to do something with it) So what systems do is, store that data into a buffer to output it (to disk or screen) later, and continue executing your program... But what if you do not want this lazy thing to happen. Say you want to tell the user - "everything is written to pen drive. safe to remove pen drive" and there is your lazy system which will do this later!! This is the place to say fflush - (short for flush file) that forces all input/output to complete. It is a common, though incorrect practice to use fflush on the input stream, in order to clear any extraneous input left behind by a scanf function. This may not work in all compilers, and should generally be avoided. File operations: fopen(), fclose(), rewind(), fseek(), fgets(), fputs() void perror(const char *s); --The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream malloc(),calloc() and realloc(): void *malloc(size_t size): Returns unintialized memory of size "size", retuns "null" if memory is not available. void *calloc(size_t nelem, size_t elsize): Returns intialized memory with 0 of nelem variables of size "elsize". void *realloc(void *ptr, size_t size): changes the size of the object pointed to by ptr to the size specified by size, and returns a pointer to the possibly moved block. The contents are unchanged up to the lesser of the new and old sizes. If ptr is null, realloc() behaves like malloc() for the specified size. If size is zero (0) and ptr is not a null pointer, the object pointed to is freed.

No comments:

Post a Comment