Monday, November 30, 2015

File in C Programming Language

Hey fellas! Now you meet me again with the different topic. Now I will explain to y'all about FILE in C programming language. Hope u will understand about FILE after learn from my post, thankyou!^^

File is an object on the computer that stores data. File is created for permanent storage of data. Files are needed if we want to keep large volume of data. With file, these information can be accesed using few commands. There are 2 kinds of file in general:
  1. Text file
    source code, plain text, html files, most of configuration files, etc.
  2. Binary file
    mosts audio and video file formats, graphics file formats, compressed files, most of office formats, etc.
In C programming language, we use a structure pointer of file type to declare a file.
The syntax is
      FILE *fp;
C programming language provides some functions that helps user to perform basic file operations.
There are the functions:
  • fopen()
    The syntax:
    FILE *fopen(const char*filename, constchar*mode);
    fopen() to create a new file or open a existing file. Opens the file whose name is specified
     in the parameter filename and associates it with a stream that can be identified in future
    operations by the FILE pointer returned.
    Here filename is the name of the file to be opened and mode specifies the purpose of opening the file. Mode can be of following types, r = opens a text file in reading mode
    w = opens or create a text file in writing modea = opens a text file in append moder+ = opens a text file in both reading and writing modew+ = opens a text file in both reading and writing modea+ = opens a text file in both reading and writing mode
    rb = opens a binary file in reading modewb = opens or create a binary file in writing modeab = opens a binary file in append moderb+ = opens a binary file in both reading and writing mode
    wb+ = opens a binary file in both reading and writing modeab+ = opens a binary file in both reading and writing mode
  • fclose()
    The syntax:
    int fclose(FILE*stream);
    fclose() to closes a file. Closes the file associated with the stream and disassociates it. Here fclose() function closes the file and returns zero on success, or EOF if there is  an error in closing the file. This EOF is a constant defined in the header file stdio.h.
  •  getc()
    The syntax:
    int getc(FILE*stream);
    getc() to reads a character from a file. Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.
  •  putc()
    The syntax:
    int putc(int character, FILE*stream);
    putc to writes a character to a file. The character is written at the position indicated by the internal position indicator of the stream, which is then automatically advanced by one.
  • fscanf()
    The syntax:
    int fscanf(FILE*stream, const char*format,...);
    fscanf() to reads a set of data from a file. Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.
  •  fprintf()The syntax:
    int fprintf(FILE*stream, const char*format,...);fprintf() to writes a set of data to a file. Writes the C string pointed by format to the stream. If format includes format specifiers, the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
  •  getw()
    getw() to reads a integer from a file.
  • putw()
    putw() to writes a integer to a file.
  • fseek()
    The syntax:
    int fseek(FILE*stream, long int offset, int origin);
    fseek() to set the position to desire point. For streams open in binary mode, the new position is defined by adding offset to a reference position specified by origin. For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily by SEEK_SET.
    SEEK_SET => Beginning of file
    SEEK_CUR => Current position of the file pointer
    SEEK_END => End of file
  • ftell()
    The syntax:
    long int ftell(FILE*stream);
    ftell() to gives current position in the file. For binary streams, this is the number of bytes from the beginning of the file. For text streams, the numerical value may not be meaningful but can still be used to restore the position to the sam position later using fseek.
  • rewind()
    The syntax:
    void rewind(FILE*stream);
    rewind() to set the position to the beginning point. On streams open for update (read+write), a call to rewind allows the switch between reading and writing. 
This is some example about FILE, i have uploaded it in my 4shared account. You can download it from FILE EXAMPLE.
That's all about FILE in C programming language. For more explanation you can find it at
File Handling in C Language
C - File I/O
http://www.cplusplus.com/reference/
Have a great time with coding guys! See ya at the next post!

Thursday, November 26, 2015

Struct

Hey, fellas! Now, I will explain about STRUCT in C programming language! Yeay! Struct or structure is a way to create a new data type that can store multiple variables. Each elements in C structure is called member. If u want to access the member of the struct in C programming language, you should be declare the structure variable. It is many structure variables can be declared for same struct and memory will be allocated for each separately.
Now, I will explain how to define a C struct? To define a struct you must use the struct statement. The syntax is:

Structure tag is optional. Member definition is a normal variable definition.
You can see the example about how to access structure members, structures as function arguments, and pointers to structures from this link http://www.tutorialspoint.com/cprogramming/c_structures.htm .

Keyword typedef while using structure. Programmer generally use typedef while using structure in C programming language. Typedef or type definitions make it possible to create your own variable types.

If you want to know more about Struct in C programming language you can visit link
http://fresh2refresh.com/c/c-structures/
http://www.codingunit.com/c-tutorial-structures-unions-typedef
http://www.programiz.com/c-programming/c-structure-function

or you can watch video from
49 | Structures in C Programming Language Video Tutorial
Structure and Unions in C Programming


Tuesday, November 24, 2015

Pointer and Reference in C Programming Language

Hey, fellas! Long time no see yaa~ In this post, I will tell u about pointer and reference in C programming language! Yeay!
Pointer
First, let me tell you "what is pointer?". Pointer is data type that can hold the address of a variable. When we should use pointer in C programming language? When we need to change a variable content on another's function, to create dynamic data stuctures, to pass and handle variable parameters passed to functions, and to access information stored in arrays.
What is the syntax to declare the pointer?

          data_type : It specifies the type of the pointer. This type specifies the type of variable
                             whose address this pointer can store.
          pointer_variable_name : It can be any name specified by the user (us). The pointer names
                                                  commonly start with 'p' or 'ptr'.
 There are some example for the valid pointer declarations:


This is an example using pointer in c


The output is



 There may be a situation when we want to maintain an array which can store pointer to another data type. How to declare array of pointer in C?


From the example above, we know that it declares ptr as an array of ARRAY integer pointers.
Now I will show you the example about array of pointer.
 
The output will show
l
C programming allows passing a pointer to a function also allows returning a pointer to a function.
To allows passing a pointer to a function is so simple, just declare the function parameter as a pointer type. To returning a pointer to a function, you will have to declare a function returning a pointer. It is so bad to return the address of a local variable outside the function.

References operator (&)
If x is a variable then, &x is the address in memory.
You have already used reference operator in C programming language while using scanf() function.

This is a video for help your learning about pointer
C programming Pointers fully explained