Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Dictionary Word Search C Program

How to write a C Program to Find Dictionary Word Search ?


Solution For C Program to Find a Word in Dictionary :

C Program to copy a file to another file

How to write a C Program to copy a file to another file in C Programming Language ?

This C Program to copy a file to another file.

Solution:

  1. #include<stdio.h>
  2. void main()
  3. {
  4.         FILE *fp1,*fp2;
  5.         char ch;
  6.         char fname1[30],fname2[30];
  7.         printf("Enter the source file: ");
  8.         fflush(stdin);
  9.         scanf("%s",fname1);
  10.         printf("Enter the destination file: ");
  11.         fflush(stdin);
  12.         scanf("%s",fname2);
  13.         fp1=fopen(fname1,"r");
  14.         fp2=fopen(fname2,"w");
  15.         if(fp1==NULL)
  16.         {
  17.                 printf("\n cannot open file %s for reading",fname1);
  18.                 exit(1);
  19.         }
  20.         else if(fp2==NULL)
  21.         {
  22.                 printf("\n cannot open file %s for writing",fname2);
  23.                 exit(1);
  24.         }
  25.         else
  26.         {
  27.                 ch=getc(fp1);
  28.         while(ch!=EOF)
  29.         {
  30.                 putc(ch,fp2);
  31.                 ch=getc(fp1);
  32.         }
  33.         fclose(fp1);
  34.         fclose(fp2);
  35.         printf("\n files copied");
  36.         }
  37. }

C Program to Find the Size of File using File Handling Function

How to write a C Program to Find the Size of File using File Handling Function in C Programming Language ?

Solution:



/*
 * C Program to Find the Size of File using File Handling Function
 */
#include <stdio.h>
 
void main(int argc, char **argv)
{
    FILE *fp;
    char ch;
    int size = 0;
 
    fp = fopen(argv[1], "r");
    if (fp == NULL)
        printf("\nFile unable to open ");
    else 
        printf("\nFile opened ");
    fseek(fp, 0, 2);    /* file pointer at the end of file */
    size = ftell(fp);   /* take a position of file pointer un size variable */
    printf("The size of given file is : %d\n", size);    
    fclose(fp);
}

C Program Read a char & print next char ( file to file )

How to write a c program to read a char & print next char ( file to file ) in C Programming Language ?


Solution:
/* Read a char & print next char ( file to file ) : A character should be written in the file named "readfile.txt" beforehand and we will take this as input to be manipulated and finally written into the file named "writefile.txt" */

QuickVSInsertion Sorting C Program

How to write a C Program to QuickVSInsertion Sorting C Programming Language


Solution:
#include <stdio.h>
#include <stdlib.h> //usado para a fx system("cls"); que limpa a tela
#include <windows.h>
#include <conio.h> //no caso se eu precisar de pausar a tela

File Handling (console to file) in C Program

How to write a C Program Writing a character into a file,reading the character from the console and writing the next letter into the file in C Programming Language ?



Solution:
/* Writing a character into a file,reading the character from the console and writing the next letter into the file*/

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

int main()
{

FILE *fp; //creates a file pointer
char ch;
int con;


fp=fopen("filehandling.txt","w+"); // creates a text file with label "filehandling" in write mode

if(fp==NULL)  //checks for the filename validity
{
printf("\nUnable to open the file\n");
exit(0);
}
printf("\nFile opened successfully\n");

printf("\nEnter the character as input\n");
scanf("%c",&ch);


putc(ch,fp);

fclose(fp);

fp=fopen("filehandling.txt","rwa+");

ch=getc(fp);


con=ch;
con++;
if(con>=97 && con<122)
{

fprintf(fp,"\nthe next char is %c\n",con);
}
else
{
fprintf(fp,"\nthe next char is a\n");
fclose(fp);

// open filehandling.txt file and check the input and output given...
}
}