The Big Indian Loop
Let's Learn Coding
Monday, 9 March 2020
Program for Largest Number among three numbers
Program for Largest Number among three numbers
Program to get ASCII of a character in C
int main()
{
char ch;
printf("Enter the character: "); //input character
scanf("%c",&ch);
printf("ASCII is = %d\n", ch);
return 0;
}
Saturday, 7 March 2020
File Handling in C
File Input/Output : C files I/O functions handle data on a secondary storage device, such as a hard disk.
C can
handle files as Stream-oriented data (Text) files, and System
oriented data (Binary) file.
In C language, we use a structure pointer of file type to declare a
file. FILE *fp;
C
provides a number of functions that helps to perform basic file
operations.
Following
are the functions.
|
Function |
Description |
|
fopen() |
create
a new file or open a existing file |
|
fclose() |
closes
a file |
|
getc() |
reads
a character from a file |
|
putc() |
writes
a character to a file |
|
fscanf() |
reads
a set of data from a file |
|
fprintf() |
writes
a set of data to a file |
|
getw() |
reads
a integer from a file |
|
putw() |
writes
a integer to a file |
|
fseek() |
set
the position to desire point |
|
ftell() |
gives
current position in the file |
|
rewind() |
set
the position to the beginning point |
C File Operations : Five major operations can be performed on file are:
- Creation of a new file.
- Opening an existing file.
- Reading data from a file.
- Writing data in a file.
- Closing a file.
Opening a
File or Creating a File
The fopen() function is used to create a new
file or to open an existing file.
General Syntax:
*fp = FILE *fopen(const char *filename, const char *mode);
Here, *fp is the FILE pointer (FILE *fp), which will hold the reference to
the opened(or created) file.
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.
|
Mode |
Description |
|
r |
opens a text file in reading mode |
|
w |
opens or create a text file in
writing mode. |
|
a |
opens a text file in append mode |
|
r+ |
opens a text file in both reading and
writing mode |
|
w+ |
opens a text file in both reading and
writing mode |
|
a+ |
opens a text file in both reading and
writing mode |
|
rb |
opens a binary file in reading mode |
|
wb |
opens or create a binary file in
writing mode |
|
ab |
opens a binary file in append mode |
|
rb+ |
opens a binary file in both reading
and writing mode |
|
wb+ |
opens a binary file in both reading
and writing mode |
|
ab+ |
opens a binary file in both reading
and writing mode |
Closing a
File
The fclose() function
is used to close an already opened file.
Syntax : int
fclose( FILE *fp);
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.
Input/Output operation on File
Example:
#include<stdio.h>
int
main()
{
FILE
*fp;
char
ch;
fp
= fopen("one.txt", "w");
printf("Enter
data...");
while( (ch = getchar()) != EOF)
{
putc(ch,
fp);
}
fclose(fp);
fp
= fopen("one.txt", "r");
while(
(ch = getc(fp)! = EOF)
printf("%c",ch);
// closing the file
pointer
fclose(fp);
return 0;
}
Reading and Writing to File
using fprintf() and fscanf()
Example:
#include<stdio.h>
struct
emp
{
char
name[10];
int age;
};
void
main()
{
struct
emp e;
FILE
*p,*q;
p
= fopen("one.txt", "a");
q
= fopen("one.txt", "r");
printf("Enter
Name and Age:");
scanf("%s
%d", e.name, &e.age);
fprintf(p,"%s
%d", e.name, e.age);
fclose(p);
do
{
fscanf(q,"%s
%d", e.name, e.age);
printf("%s
%d", e.name, e.age);
}
while(!feof(q));
}
In this
program, we have created two FILE pointers and both are referring to
the same file but in different modes.
fprintf() function directly writes
into the file, while fscanf() reads from the file, which can then be printed on the
console using standard printf() function.
Reading and
Writing in a Binary File : A Binary
file is similar to a text file, but it contains only large numerical data. The
Opening modes are mentioned in the table for opening modes above.
fread() and fwrite() functions
are used to read and write is a binary file.
Syntax:
fwrite(data-element-to-be-written, size_of_elements, number_of_elements, pointer-to-file);
fread() is also used in the same way, with the same arguments like fwrite() function.
Below
mentioned is a simple example of writing into a binary file
Example:
const
char *mytext = "The quick brown fox jumps over the lazy dog";
FILE
*bfp= fopen("test.txt", "wb");
if (bfp)
{
fwrite(mytext,
sizeof(char), strlen(mytext), bfp);
fclose(bfp);
}
fseek(), ftell() and rewind() functions
fseek() : It is used to move the reading control to different positions using fseek function.
ftell() : It tells the byte location of current position of cursor in file pointer.
rewind() : It
moves the control to beginning of the file.
Pointers in C
Operators that are used with Pointers
"Address of" (&) Operator
"Value at Address" (*) Operator
|
ip = ip + 1 |
ip = ip + 1 = 1000 + 1*4 = 1004 |
|
ip++ or ++ip |
ip++ = ip + 1 = 1004 + 1*4 = 1008 |
|
ip = ip + 5 |
ip = ip + 5 = 1008 + 5*4 = 1028 |
|
ip = ip - 2 |
ip = ip - 2 =1028
- 2*4 = 1020 |
|
ip-- or --ip |
ip = ip + 2 = 1020 + 2*4 = 1028 |
|
dp
+ 1 |
dp
= dp + 1 =2000 + 1*8 = 2008 |
|
dp++ or ++dp |
dp++ = dp+1 = 2008+1*8 = 2016 |
|
dp
= dp + 5 |
dp = dp
+ 5 = 2016+5*8 =2056 |
|
dp
= dp - 2 |
dp = dp
- 2 = 2056-2*8 = 2040 |
|
dp-- or --dp |
dp = dp
- 1 = 2040-1*8 = 2032 |
Pointer Arithmetic on Characters
|
cp + 1 |
cp = cp + 1 = 3000 +
1*1 = 3001 |
|
cp++ or ++cp |
cp = cp +
1 = 3001 + 1*1 = 3002 |
|
cp = cp + 5 |
cp = cp +
5 = 3002 + 5*1 = 3007 |
|
cp = cp - 2 |
cp = cp +
5 = 3007 - 2*1 = 3005 |
|
cp-- or --cp |
cp = cp +
2 = 3005 - 1*1 = 3004 |
Pointer to Array
Program for Largest Number among three numbers
Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h> in...
-
Storage Classes : A storage class defines the scope (visibility) and life-time of variables and/or functions within a C program. We hav...

