Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Thursday, January 19, 2017

Read A Text File Using C

Read a text file using C

Open a text file using C program and read the content of the file.


Code

/*Write a program to read a file*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
 FILE *fp; //Taking a file pointer variable
 char fname[30],ch;

 //Taking a existing file name from the user to read
 printf("\nEnter a file name to read: ");
 gets(fname);

 //Open the file in read mode
 fp=fopen(fname,"r");
 
 //Check the file is corrupted or not
 if(fp==NULL)
 {
  printf("\nFile cannot be access.");
  exit(0);
 }
 if(ferror(fp)!=0)
 {
  printf("\nFile is either corrupted or damaged");
  exit(1);
 }

 //Print the content of the file
 while((ch=getc(fp))!=EOF)
 {
  printf("%c",ch);
 }

 //Close the file
 fclose(fp);

 return 0;
}

Output>

Enter a file name to read: temp.txt
This is a temporary file.
Note:
To read a file you have to create a file first by using any text editor(Notepad) or you can also use the C program to create a file.

C Program to Store Record In A File

C program to store students records in a file

The main purpose of this program is that to store some information in a file from console.

Code

/*Write a program that will read roll, name and marks of several student from the user and store them in a file*/
#include <stdio.h>
#include <stdlib.h>
//Create a structure for store the records for all students
struct student
{
 int roll;
 char name[30];
 int marks;
} s;
int main( ){
 FILE *fp; //Create a file pointer variable
 char fname[20],s2[20];
 int n,i,s1,s3;
 printf("\nEnter a file name to store student record: ");
 gets(fname);
 //Open the file in write mode
 fp=fopen(fname,"w");
 if(fp==NULL){
  printf("\nError:FIle cannot be created. \a");
  exit(0);
 }
 printf("\nEnter the number of student: ");
 scanf("%d",&n);
 //Taking the records of students and store them in the file
 for(i=0;i<n;i++){
  printf("\nFor student no. %d", i+1);
  printf("\nEnter roll: ");
  scanf("%d",&s.roll);
  printf("\nEnter name: ");
  scanf("%s",&s.name);
  printf("\nEnter marks: ");
  scanf("%d",&s.marks);
  fprintf(fp,"%d\t%s\t%d\n",s.roll,s.name,s.marks);
 }
 //Close the file
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nError:FIle does not exist. \a");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nError: File is either corrupted or damaged. \a");
  exit(0);
 }

 //Print the content of the file
 for(i=1;i<=n;i++){
  fscanf(fp,"%d\t%s\t%d\n",&s1,&s2,&s3);
  printf("\n%d\t%s\t%d",s1,s2,s3);
 }
 //Close the file
 fclose(fp);
 return(0);
}

Output

Enter a file name to store student record: student.txt
Enter the number of student: 3
For student no. 1
Enter roll: 101
Enter name: Alex
Enter marks: 85
For student no. 2
Enter roll: 102
Enter name: Rose
Enter marks: 96
For student no. 3
Enter roll: 103
Enter name: Charls
Enter marks: 93
The content of the file is:
101     Alex    85
102     Rose    96
103     Charls  93

Replace A Word In A File

Replace a word in a file

Find a specific word from an existing file and replace that word with another.

Code

/******Word replace in a file******/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
 FILE *fp,*fp1;
 char str[15],str1[15],fname[20],ch;
 int i=0,len,flag,x=0;
 //Creating a file
 printf("\nEnter a file name:");
 gets(fname);
 fp=fopen(fname,"w");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Enter the content in the file
 printf("\nEnter the content of the file. press ctrl+z after compulsion.\n\n");
 while((ch=getchar())!=EOF)
  putc(ch,fp);
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Print the content of the file
 printf("\nThe content of the current file is:\n");
 while((ch=getc(fp))!=EOF)
  printf("%c",ch);
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Taking input of the word to be replace
 printf("\nEnter the word you want to replace: ");
 scanf("%s",str);
 len=strlen(str);
 printf("\nEnter replaced word: ");
 scanf("%s",str1);
 //Open a new file to copy all the content of the old file with the replace word
 fp1=fopen("new.txt","w");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 while((ch=getc(fp))!=EOF){
  if((ch!=32) && (ch!='\n')){
   flag=0; 
   i=0;
   do{
    if((ch==str[i]) && (i<len))
     i++;
    else{
     flag=1;
     break;
    }   
   }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!'));
  
   if(flag==0){
    fprintf(fp1,"%s",str1);
    if(ch==32) putc(ch,fp1);
    if(ch=='\n') putc(ch,fp1);
    if(ch=='?') fprintf(fp1,"%c ",ch);
    if(ch=='!') fprintf(fp1,"%c ",ch);
    if(ch=='.') fprintf(fp1,"%c ",ch);
    x++;
   }
   else{
    if(i!=0)
     fseek(fp,-(i+1),SEEK_CUR);
    do{
     putc(ch,fp1);
    }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!'));
    if(ch==32) putc(ch,fp1);
    if(ch=='\n') putc(ch,fp1);
    if(ch=='?') fprintf(fp1,"%c ",ch);
    if(ch=='!') fprintf(fp1,"%c ",ch);
    if(ch=='.') fprintf(fp1,"%c ",ch);
   }
  }
 }
 printf("\n%d word(s) are found and replace successfully.",x);
 fclose(fp);
 fclose(fp1);

 fp1=fopen("new.txt","r");
 printf("\nThe content of the new file is:\n");
 while((ch=getc(fp1))!=EOF)
  printf("%c",ch);
 
 fclose(fp1);
 return 0;
}

Output


Enter a file name:temp.txt
Enter the content of the file. press ctrl+z after compulsion.
This is a temp file. This file is for testing.
^Z
The content of the current file is:
This is a temp file. This file is for testing.
Enter the word you want to replace: file
Enter replaced word: program
2 word(s) are found and replace successfully.
The content of the new file is:
This is a temp program. This program is for testing.

C Program to Count From A File

C program to count from a file

Count the numbers of characters, vowels, consonant, lines, words, punctuation marks from a existing file.


Code

/*Write a program that will read the contains of a file and count the characters, vowels, consonant, lines, words, punctuation marks */
#include <stdio.h>
#include <stdlib.h>

int main()
{
 FILE *fp;
 char fname[30],ch;
 int c=0,v=0,ln=0,w=0,p=0;

 //Taking a file name from the user
 printf("\nEnter a file name to read: ");
 gets(fname);

 //Open the file in read mode
 fp=fopen(fname,"r");
 
 //Check is the file exist or not
 if(fp==NULL)
 {
  printf("\nFile cannot be access.");
  exit(0);
 }

 //Print the content of the file
 printf("\nThe contain of the file is: \n");
 while((ch=getc(fp))!=EOF)
 {
  printf("%c",ch);
  
  //Checking and count the outputs
  if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))
  {
   c=c+1;
  }
  if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
  {
   v=v+1;
  }
  if((ch=='.')||(ch=='?')||(ch=='!'))
  {
   ln=ln+1;
   w=w+1;
  }
  if(ch==' ')
  {
   w=w+1;
  }
  if((ch==';')||(ch=='"')||(ch==','))
  {
   p=p+1;
  }
 }
 //Print the outputs
 printf("\nNumber of character in the file is %d",c);  //Count the characters
 printf("\nNumbers of vowels in the file is %d",v);               //Count the vowels
 printf("\nNumbers of consonant in the file is %d",(c-v));       //Count the consonants
 printf("\nNumbers of lines in the file is %d",ln);              //Count the lines
 printf("\nNumbers of words in the file is %d",w);               //Count the words
 printf("\nNumbers of punctuation marks in the file is %d",p);   //Count the punctuation marks
 fclose(fp);

 return 0;
}

Output

Enter a file name to read: temp.txt

The containt of the file is:
This is a temporary file.

Number of charecter in the file is 20
Numbers of vouls in the file is 8
Numbers of consonent in the file is 12
Numbers of lines in the file is 1
Numbers of words in the file is 5
Numbers of punchuation marks in the file is 0

Monday, January 16, 2017

Create File In C

Create file in C

Create a text file in C and enter some content in it and close the file.


Code

/****Write a program to create a file****/
#include <stdio.h>
#include <stdlib.h>

int main()
{

 FILE *fp; //Taking a file pointer variable
 char fname[30],ch;

 //Taking a file name from the user with its extension
 printf("\nEnter a file name: ");
 gets(fname);

 //Open the file in write mode
 fp=fopen(fname,"w");
 if(fp==NULL)
 {
  printf("\nFile cannot be created.");
  exit(0);
 }

 //Write the content from the console to the file
 printf("\nEnter contain of the file: \n");
 while((ch=getchar())!=EOF)
 {
  putc(ch,fp);
 }

 printf("\nFile created successfully.");
 //Close the file
 fclose(fp);

 return 0;
}

Output

Enter a file name: temp.txt

Enter contain of the file:
This is a temporary file.
^Z

File created successfully.