C programming:-Files - Part 5

/*         Explain how do you create , open a file. WAP to read from a keyboard and write to a file and display its contents. Give examples.*/

#include<stdio.h>
main()
{
            FILE *fp;
            char s[30],s1[30];
            printf("enter a string:");
            gets(s); //read a string from terminal
            fp=fopen("file1.txt","w");
            fprintf(fp,"%s",s);//write string s to file1.txt
            fclose(fp);
            //display file contents
            printf("file contents are: ");
            fp=fopen("file1.txt","r");
            fgets(s1,30,fp); //reads data from file to a string s1
            printf("%s",s1);// display string s1 to the terminal
}
/*OUTPUT
enter a string:hello students
file contents are: hello students
*/
WRITE A C PROGRAM TO MERGE TWO FILES?

#include<stdio.h>
#include<conio.h>
void main()
{
 FILE *fp,*fp1;
 char ch;
 clrscr();
 fp=fopen("f1.txt","r");
 fp1=fopen("merge.txt","w");
 printf("first file content:\n");
 ch=getc(fp);
 printf("%c",ch);
 while(ch!=EOF)
 {
  putc(ch,fp1);
  ch=getc(fp);
  printf("%c",ch);
 }
 fclose(fp);
 fp=fopen("f2.txt","r");
 printf("\n\nsecond file content:\n");
 ch=getc(fp);
 printf("%c",ch);
 while(ch!=EOF)
 {
  putc(ch,fp1);
  ch=getc(fp);
  printf("%c",ch);
 }
 fclose(fp);
 fclose(fp1);
 printf("\n\nmearged file:\n");
 fp1=fopen("mearge.txt","r");
 while((ch=getc(fp1))!=EOF)
 {
  printf("%c",ch);
 }
 fclose(fp);
 getch();
}
WAP that reads 50 numbers from the user. The pgm may use functions to find the Fibonacci numbers in it and write in to a file. The  pgm should display the contents of file at the end.(JAN 2007)
#include<stdio.h>

void checkfib();
int ar[50],m,n,br[30],cr[30];
int i,j;
int a,b,s;
FILE *f;
int c=0;
main()
{
            a=0;ar[0]=a;
            b=1;ar[1]=b;
            s=a+b; i=2;
            while(s<=100)
            {
                        ar[i]=s;
                        //printf("%d",ar[i]);
                        a=b;
                        b=s;
                        s=a+b;i++;

            }
            m=i;
            for(j=0;j<m;j++)
                        printf("%d ",ar[j]);
            printf("\nenter limit");
            scanf("%d",&n);
            printf("\nenter %d numbers:",n);
            for(i=0;i<n;i++)
            {
                        scanf("%d",&br[i]);
            }
            checkfib();

}
void checkfib()
{
            printf("fibonacci numbers are:");
            f=fopen("fib.txt","w");
            for(i=0;i<n;i++)
            {
                        for(j=0;j<m;j++)
                        {
                                    if(br[i]==ar[j])
                                    {
                                               
                                                printf("%d ",br[i]);
                                                fprintf(f,"%d ",br[i]);c++;
                                                 i++;

                                    }
                        }
            }
            fclose(f);
            printf("file details are:");
            //read from file
            f=fopen("fib.txt","r");
            for(i=0;i<c;i++)
            {fscanf(f,"%d",&cr[i]);
            fprintf(stdout,"%d ",cr[i]);}
            fclose(f);
}
/*OUTPUT
student@ubuntu:~$ ./a.out
0 1 1 2 3 5 8 13 21 34 55 89
enter limit10

enter 10 numbers:1 2 3 4 5 6 7 8 9 10
fibonacci numbers are:1 2 3 5 8 file details are:1 2 3 5 8 */



Comments

Popular posts from this blog

Hibernate Collection Mapping example

Struts 2 Troubleshooting