Jumat, 27 November 2015

File in C

Hello readers!

i want to show you about File in C.

File

  • Place to save the collection of data record.
  • Represents a sequence of bytes, regardless of it being a text file or a binary file.

C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

Opening Files

We use fopen() to create a new file or open a file.

FILE *fopen( const char * filename, const char * mode );

filename: string liberal, used to name our file
mode: 
ModeDescription
rOpens an existing text file for reading purpose.
wOpens a text file for writing. If it does not exist, then a new file is created. 
aOpens a text file for writing in appending mode. If it does not exist, then a new file is created.
r+Opens a text file for both reading and writing.
w+Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

Closing Files

We use fclose() function to close a file.

int fclose( FILE *fp );

fclose(-) function returns zero on success, or EOF if there is an error in closing the file.

Writing Files

int fputc( int c, FILE *fp );

The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error.

Reading Files

int fgetc( FILE * fp );

The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOFThe following function allows to read a string from a stream:

char *fgets( char *buf, int n, FILE *fp );

The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a nullcharacter to terminate the string.
You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character.

Example:



That's the Structs in C. Hope this will help you. Thanks! :D

nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/cprogramming/c_file_io.htm




Kamis, 26 November 2015

Structs in C

Hello readers!

i want to show you about Struct in C.

Struct

  • Another user defined data type available in C that allows to combine data items of different kinds.


Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about peoples name, age and their citizenship. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Syntax of structure:

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type member;

};


We can create the structure for a person as mentioned above as:

struct person
{
    char name[5];
    int cit_no, age ;
    

};

This declaration above creates the derived data type struct person.


Relation between Struct and Array

Sometimes, we will do a task with data that has some item, but it differs from array. For example, identity card has data like name, adress, sex, and age. That is not suitable if we use array, because array only can has some element with the same type. That example can be used in a struct.

Example:




Relation between Struct and Function

You can pass a structure as a function argument in the same way as you pass any other variable or pointer.

Example:



That's the Structs in C. Hope this will help you. Thanks! :D
nb: Some source that really helps me very much (recommended):
http://www.programiz.com/c-programming/c-structures
http://stackoverflow.com/questions/10370047/passing-struct-to-functio
http://www.tutorialspoint.com/cprogramming/c_structures.htm



Selasa, 24 November 2015

Pointer & Reference in C

Hello readers!

i want to show you about Pointer&Reference in C.

Pointer

  • A variable that contains an adress from another variable

Pointer, based on it's name, used to point. What is appointed? the answer is a data. Pointer is can be imagined as a normal variable, but it doesn't save data, it saves an adress of data. That's why Pointer is used to point.

Example of Pointer: 



Relation between Pointer and Array

An array name is a constant pointer to the first element of the array.

Example of Array Pointer:



Relation between Pointer and Function

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

There is an example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function 




Reference

Actually, a reference is not really like a pointer.
A compiler keeps "references" to variables, associating a name with a memory address; that's its job to translate any variable name to a memory address when compiling.
When you create a reference, you only tell the compiler that you assign another name to the pointer variable; that's why references cannot "point to null", because a variable cannot be, and not be.
Pointers are variables, they contain the address of some other variable, or can be null. The important thing is that a pointer has a value, while a reference only has a variable that it is referencing.
Now some explanation of real code:
int x = 0;
int& y = x;
That's the Pointer & Reference in C. Hope this will help you. Thanks! :D

nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/cprogramming/c_pointers.htm
http://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm
http://www.cprogramming.com/tutorial/function-pointers.html
http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in