C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<process.h>
long file_size(FILE *fp)
{
long len;
//move file pointer to end of the file */
fseek(fp, 0L, 2);
//Obtain the current position of file pointer
len = ftell(fp);
return len;
}
void main(void)
{
FILE *fp;
char filename[20];
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found!nn");
exit(0);
}
printf("File size of %s is %ld bytesnn",filename, file_size(fp));
fclose(fp);
}

Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-8.

K and R C, Solution to Exercise 7-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to print a set of files, starting each new one on a new page, with a title and running page for each file Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>

#define MAXBOT 3
#define MAXHDR 5
#define MAXLINE 100
#define MAXPAGE 66

/*print: print files - each new one on a new page */
main(int argc, char *argv[])
{

FILE *fp;
void fileprint(FILE *fp, char *fname);

if(argc == 1)
fileprint(stdin," ");
else
if((fp = fopen(*++argv,"r")) == NULL) {
fprintf(stderr,"find:can't open %sn",*argv);
exit(1);
} else {

fileprint(fp, *argv);
fclose(fp);
}
return 0;
}


//fileprint: print file name
void fileprint(FILE *fp, char *fname)
{
int lineno, pageno = 1;
char line[MAXLINE];
int heading(char *fname, int pageno);
lineno = heading(fname, pageno++);
while(fgets(line, MAXLINE, fp) != NULL) {
if(lineno == 1) {
fprintf(stdout,"f");
lineno = heading(fname, pageno++);
}
fputs(line, stdout);
if(++lineno > MAXPAGE - MAXBOT)
lineno = 1;
}
fprintf(stdout,"f");
}

//heading: put heading and enough blank lines
int heading(char *fname, int pageno)
{
int ln = 3;
fprintf(stdout,"nn");
fprintf(stdout,"%s page %dn", fname, pageno);
while(ln++ < MAXHDR)
fprintf(stdout,"n");
return ln;
}
Read more c programs

C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 7-6.

K and R C, Solution to Exercise 7-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C Program to compare two files, printing the first line where they differ.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXLINE 100
/*comp: compare two file, printing the first line where they differ*/
main(int argc, char *argv[])
{
FILE *fp1, *fp2;
void filecomp(FILE *fp1, FILE *fp2);
if(argc != 3){
fprintf(stderr,"comp:need two file namesn");
exit(1);
} else {
if((fp1 = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "comp:can't open %sn",*argv);
exit(1);
} else if((fp2 = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "comp:can't open %sn",*argv);
exit(1);
}else {
filecomp(fp1,fp2);
fclose(fp1);
fclose(fp2);
exit(0);

}
}
}

//filecomp: compare two files -a line at a time
void filecomp (FILE *fp1, FILE *fp2)
{
char line1[MAXLINE], line2[MAXLINE];
char *lp1 = *lp2;
do{
lp1 = fgets(line1, MAXLINE, fp1);
lp2 = fgets(line2, MAXLINE, fp2);
if(lp1 == line1 && lp2 == line2){

if(strcmp(line1,line2) !=0) {
printf("First difference in linen%sn",line1);
lp1 = lp2 = NULL;
}
} else if(lp1 != line1 && lp2 == line2)
printf("end of first file at linen%sn",line2);
else if(lp1 == line1 && lp2 == line2)
printf("end of second file at line n%sn",line1);

}while(lp1 == line1 && lp2 == line2);
}



Read more c programs

C Basic
C File i/o
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program which produces its own source code as its output.

Write a c program which produces its own source code as its output.
This C program uses the C File i/o operations like fopen(), fclose(). In this program, We pritn the source code of the program.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
int main()
{
FILE *fp;
char c;

fp = fopen(__FILE__,"r");

do{

c= getc(fp);

putchar(c);

}
while(c!=EOF);

fclose(fp);

return 0;

}


Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to count number of characters in the file

C Program to count number of characters in the file. In this program you can learn c file operations. Here we counting the characters by reading the characters in the file one by one and if read character was not an ‘n’ ,’t’ or EOF, it increments the counter by one. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
printf("File can't be createda");
getch();
exit(0);
}
printf("Enter some text and press enter key:n");
while((ch=getche())!='r')
{
fputc(ch,fptr);
}
fclose(fptr);
fptr=fopen("text.txt","r");
printf("nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("nThe number of characters present in file is: %d",count);
getch();
}

Read more Similar C Programs
C Basic

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to demonstrate the ‘fgets’ function.

Program to demonstrate the ‘fgets’ function. The program will count the number of lines in a file. This is a function of the UNIX command ‘wc’. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>

#define LINE_LENGTH 80

main()
{
FILE* fp;
char line[LINE_LENGTH];
int count=0;

fp=fopen("/home/DOC/C/c.html","r");
/* Count up the lines here. */
while ( fgets(line, LINE_LENGTH, fp) != NULL) count++;

printf("File contains %d lines.n", count);

fclose(fp);
}
Read more Similar C Programs
C Strings

Simple C Programs

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to do the operations of Sequential file with records.

C Program to do the operations of Sequential file with records. Sequential file, A file which consists of the same record types that is stored on a secondary storage device. The physical sequence of records may be based upon sorting values of one or more data items or fields. Here we are creating, Reading, searching the sequential file, using c file i/o operations. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
typedef struct
{
int usn;
char name[25];
int m1,m2,m3;
}STD;

STD s;

void display(FILE *);
int search(FILE *,int);

void main()
{
int i,n,usn_key,opn;
FILE *fp;
printf(" How many Records ? ");
scanf("%d",&n);
fp=fopen("stud.dat","w");
for(i=0;i<n;i++)
{
printf("Read the Info for Student: %d (usn,name,m1,m2,m3) n",i+1);
scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen("stud.dat","r");
do
{
printf("Press 1- Displayt 2- Searcht 3- Exitt Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("n Student Records in the File n");
display(fp);
break;
case 2: printf(" Read the USN of the student to be searched ?");
scanf("%d",&usn_key);
if(search(fp,usn_key))
{
printf("Success ! Record found in the filen");
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
else
printf(" Failure!! Record with USN %d not foundn",usn_key);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!n");
break;
}
}while(opn != 3);
fclose(fp);
} /* End of main() */

void display(FILE *fp)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
if( s.usn == usn_key) return 1;
return 0;
}





Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to illustrate as to how the data stored on the disk is read.

C Program to illustrate as to how the data stored on the disk is read. In this program we use the C File i/o operations to read the file from disk. fopen function open the files for reading, and fclose() closes the file. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fptr;
char filename[15];
char ch;

printf("Enter the filename to be openedn");
gets(filename);

fptr = fopen (filename, "r"); /*open for reading*/

if (fptr == NULL)
{
printf("Cannot open filen");
exit(0);
}

ch = fgetc(fptr);

while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}

fclose(fptr);
} /* End of main () */
Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program for file operations.

C program to create a file called emp.txt and store information about a person, in terms of his name, age and salary.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>

void main()
{
FILE *fptr;
char name[20];
int age;
float salary;

fptr = fopen ("emp.txt", "w"); /*open for writing*/

if (fptr == NULL)
{
printf("File does not existsn");
return;
}

printf("Enter the namen");
scanf("%s", name);
fprintf(fptr, "Name = %sn", name);

printf("Enter the agen");
scanf("%d", &age);
fprintf(fptr, "Age = %dn", age);

printf("Enter the salaryn");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2fn", salary);

fclose(fptr);
}
/*Please note that you have to open the file called emp.txt in the directory*/

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com