C Pass Addresses and Pointers
C Pass Addresses and Pointers
In this tutorial, you'll learn to pass addresses and pointers as arguments to functions with the
help of examples.
To accept these addresses in the function definition, we can use pointers. It's because pointers
#include <stdio.h>
int main()
return 0;
}
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
Run Code
#include <stdio.h>
int main()
int* p, i = 10;
p = &i;
addOne(p);
printf("%d", *p); // 11
return 0;
}
What is a File?
A file is the name of a physical memory location in the secondary storage area. The file
contains a sequence of bytes of data in the secondary storage area in the form of an
unstructured manner. In the implementation, when we were required to interact with the
secondary storage area, then recommended going for file operations. By using files, primary
memory-related data can be sent to the secondary storage area and secondary storage area
1. Standard IO Operations
2. Secondary IO Operations
Operations. Standard IO related and Secondary IO related, all predefined functions are
The entire data is lost, when a program is terminated. Storing the data in a file will preserve
your data once the program is terminated. It will take a lot of time to enter a large number of
data. However, if you’ve got a file containing all the information, you’ll easily access the
contents of the file using a few commands in C. Without any prior changes, you can easily
Types of Files
When handling files, there are two sorts of files you ought to know about:
1. Text files
2. Binary files
Text files
Text files are the normal .txt files. You can easily create text files using any simple text editor
like Notepad. When you open those files, you will see all the contents within the file as plain
text. You can easily edit or delete the contents. They take minimum effort to for easily
readable, supply the smallest amount of security, and take bigger space for storing. In the
Text files, data is represented with the help of ASCII values, i.e., .txt, .c, .cpp
Binary files
Binary files are mostly the .bin files on your computer. Instead of storing data in plain text,
they store it within the binary form (0’s and 1’s). They can hold a better amount of
knowledge, aren’t readable easily, and provides better security than text files. In binary Files,
data is represented with the help of byte, i.e., .exe, .mp3, .mp4, .jpeg
Points to Remember:
1. To specify that a given file is being opened or created in “text mode” then append “t”
to the string mode. Examples: rt, wt, at, rt+, wt+, at+
2. To specify the binary mode, append “b” to the end of the string mode. Example: RB.
3. “fopen” and “fsopen” also allow that “t” or “b” to be inserted between the letter and
4. If “t” or “b” is not giving in the string, the mode is governed by “f” mode, if “f” mode
5. If “f” mode is set to O_TEXT, they are opened in text mode. These constants are
defined in fcntl.h
File Operations
3. Closing a file
Example:
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
int main()
FILE* fp;
fp = fopen("F:\.txt","w");
if(fp == NULL)
return EXIT_FAILURE;
fprintf(fp,"Welcome");
return EXIT_SUCCESS;
Output:
Points to Remember:
1. When we are opening a file, two parameters must be passed: Path and Mode
2. The file may open or may not open. If open, then it returns ADDRESS. If not, then it
returns NULL.
5. FILE is a predefined structure that is available in stdio.h. By using the FILE structure,
we can handle file properties. The size of the FILE structure is 16 bytes.
6. fp is a variable of type FILE*, which maintains the address of FILE. The size of fp is
fopen()
It is a predefined function, which is declared in stdio.h, by using this function we can open a
file in a specific path with a specific mode. It requires two arguments of type const char*. On
success, fopen() returns FILE*, on failure returns NULL. Generally, fopen() is failed to open
2. Mode is incorrect
fprintf()
By using this predefined function, we can write the content in the file. fprintf() can take any
number of arguments but the first argument must be FILE* and the remaining arguments are
of any path.
fclose()
By using this predefined function, we can close the file after saving data. fclose() requires
File Modes
Always FILE modes will indicate for what purpose the file needs to be opened or created.
1. Write
2. Read
3. Append
1. Write(w): Create a file for writing, if the file already exists, then it will override (the
old file is deleted and a new file is created). In “w” mode, whether the file exists or
NULL. When we are working with “r” mode, if the file doesn’t exist, a new file is not
constructed.
3. Append(a): Open an existing file for appending (write the data at end of the file) or
create a new file for writing if it doesn’t exist. When we are working with “a”, if file
4. w+ (write and read): Create a file for update i.e. write and read if the file already
exists then it will override. In w+ mode, if the file is available or not, always a new
file is constructed.
5. r+ (read and write): Open an existing file for update I.e. read and write. Generally,
“r+” mode is required, when we need to update existing information. In “r+” mode, if
6. a+ (w+ and r+): Open an existing file for update or create a new file for update. By
#include<stdio.h>
#include<stdlib.h>
int main()
FILE* fp;
char ch;
fp = fopen("F:\.txt","r");
if(fp == NULL)
{
return EXIT_FAILURE;
while(1)
fscanf(fp,"%c",&ch);
break;
printf("%c",ch);
fclose(fp);
return EXIT_SUCCESS;
Output:
fscanf()
It is a predefined function that is declared in stdio.h, by using this function, we can read the
data from a file. fscanf() can take any number of arguments but the first argument must be
and the remaining arguments should be scanf() function format. When we are working with
fscanf() function, it can read the entire content of the file except.
feof()
By using this function, we can find the end of the character position. It requires one argument
of type FILE* and returns an int value. When the file pointer is pointing to the EOF character
then it returns a non-zero value, if it is pointing to other than the EOF character then it returns
zero.
fgetc()
can read the data from a file including EOF characters also. It returns an int value i.e. ASCII
value of a character.
Program for Reading a line from a file and displaying it using C Language
#include <stdio.h>
int main() {
char c[1000];
FILE *fptr;
exit(1);
fclose(fptr);
return 0;
Output
#include<stdio.h>
#include<conio.h>
int main()
{
FILE* fp;
char path[50];
char str[50];
gets(path);
fp = fopen(path,”a");
if(fp == NULL)
getch();
return 1;
fflush(stdin);
gets(str);
fputs(str, fp);
fclose(fp);
return 0;
}
Output
1.txt
It is a predefined function that is declared in dos.h. By using this function, we can suspend
the program from execution. The delay() function requires one argument of type unsigned
integer i.e. milliseconds value. By using delay(), we can suspend the program for a minimum
sleep()
It is a predefined function that is declared in dos.h. By using this function, we can suspend
the program execution sleep() function requires one argument of type unsigned integer
stdout: It is a global pointer variable that is defined in stdio.h. By using this global pointer,
stderr: By using this global pointer, we can handle standard IO Related errors. When we are
fseek()
By using this predefined function, we can create the movement in the file pointer. fseek()
requires three arguments of type FILE*, long integer, and an integer type.
Where stream will provide file information, Offset is the number of bytes, and whence value
is file pointer location. The whence value can be recognized by using the following constant
values:
1. SEEK_SET: This constant will pass the file pointer to the beginning of the file.
2. SEEK_CUR: This constant will provide the constant position of the file pointer.
3. SEEK_END: This constant value will send the file pointer to the end of the file.
1. SEEK_SET value is 0.
2. SEEK_CUR value is 1.
3. SEEK_END value is 2.
rewind()
By using this predefined function, we can send the control to the beginning of the file.
By using this predefined function, we can find the size of the file. ftell() requires one
argument of type FILE* and returns a long integer value. Generally, ftell() returns file pointer
position, so if the file pointer is pointing to the end of the character then it is equal to the size
of the file.
remove()
By using this predefined function, we can delete two files permanently from the hard disk.
remove() requires one argument of type constant char* and returns an int value.
rename()
By using this predefined function, we can change the name of an existing file. rename()
function requires two types of arguments of type const char and returns an int value.
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
FILE *fp;
char path[50];
char ch;
gets (path);
if (fp == NULL)
return 1;
while (1)
ch = fgetc (fp);
if (feof (fp))
break;
str[i++] = ch;
}
str[i] = '\o';
fclose (fp);
remove (path);
strrev (str);
fclose (fp);
free (str);
str = NULL;
return 0;
Output
1.txt
Program to Find and Update (or Replace) a Character in a File using C Language
#include<stdio.h>
#include<stdlib.h>
int main()
FILE *fp;
char path[50];
gets (path);
if (fp == NULL)
return 1;
}
printf ("\n Enter a source character(s) : ");
fflush (stdin);
fflush (stdin);
while (1)
ch = fgetc (fp);
if (ch == EOF)
break;
if (ch == sch)
fclose (fp);
return 0;
}
Output
2.txt
#include<stdio.h>
#include<conio.h>
int main()
FILE *fp;
char path[30];
char ch;
gets (path);
if (fp == NULL)
getch ();
return 1;
do
if (flag == 1)
code = 40;
else
code = -40;
while (1)
ch = fgetc (fp);
if (ch == EOF)
break;
fclose (fp);
return 0;
}
Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define f1 "F:\\part1.mp3"
#define f2 "F:\\part2.mp3"
#define f3 "F:\\part3.mp3"
#define f4 "F:\\part4.mp3"
int main(void)
FILE *sfp;
FILE *dfp;
char spath[30];
int i = 0;
char ch;
gets (spath);
if (sfp == NULL)
return EXIT_FAILURE;
if (dfp == NULL)
fclose (sfp);
return EXIT_FAILURE;
while (1)
ch = fgetc (sfp);
if (feof (sfp))
break;
++nb;
fclose (dfp);
nb = 0;
++i;
dfp = fopen (dpath[i], "wb");
if (dfp == NULL)
fclose (sfp);
return EXIT_FAILURE;
fclose (sfp);
fclose (dfp);
return EXIT_SUCCESS;
Output
Program to combine two or more audio files together using C Language
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define f1 "F:\\part1.mp3"
#define f2 "F:\\part2.mp3"
#define f3 "F:\\part3.mp3"
#define f4 "F:\\part4.mp3"
int main(void)
FILE *sfp;
FILE *dfp;
char dpath[30];
int i = 0;
char ch;
gets (dpath);
if (dfp == NULL)
{
return EXIT_SUCCESS;
if (sfp == NULL)
fclose (dfp);
return EXIT_FAILURE;
while (1)
ch = fgetc (sfp);
if (feof (sfp))
break;
}
fclose (sfp);
fclose (dfp);
return EXIT_SUCCESS;