code for file handling operation in C for window OS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saya b
    New Member
    • Jan 2012
    • 2

    code for file handling operation in C for window OS

    I am new in C. My code is as follows,which writes and then reads integer into/from a file. But its not running.Can i get help in finding out the problem in it. Also i need to do the read and write operation with floating point no.s which i am unable to do using putw() and getw(). Is there any other function for this?

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	FILE *fp,*f2;
    	int i,n,x[100],num;
    	int add,mul,y[10];
    	int h[10]={5,2,3,6,1};
    	clrscr();
    	printf("contents of DATA file");
    	fp=fopen("DATA.c","w");
    
    /*writing integers as an array into "data" file*/
    	for(i=0;i<100;i++)
    	{
    		n=i;
    		x[n]=2*i;
    		putw(x[n],fp);
    	}
    
    /*printing the written data*/
    	while ((num=getw(fp))!=EOF)
    		printf("%d",num);
    	fclose(fp
    	);
    	fp=fopen("DATA.c","r");
    	f2=fopen("result.c","w");
    
    /*using the file "data" for further processing*/
    	while((num==getw(fp))!=EOF)
    	{
    /*logic for convolution*/
    		for(n=0;n<=104;n++)
    		{
    			add=0;
    			for(i=0;i<=n;i++)
    			{
    				mul=num*h[n-i];
    				add=mul+add;
    			}
    			y[n]=add;
    			putw (y[n],f2);
    		}
    	}
    	fclose(fp);
    	fclose(f2);
    
    /*printing the contents of file "result"*/
    	f2=fopen("result.c","r");
    	while((num=getw(f2))!=EOF)
    	printf("%d",num);
    	fclose(f2);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your integers are written as a char array unless you are wrting in binary mode.

    I suggest you write in text mode. Insert a space between each integer vaue so you can use fscanf or fgets to read.

    You wileed this technique for floating point since floating point cannot be directly read or written.

    Comment

    • saya b
      New Member
      • Jan 2012
      • 2

      #3
      Code:
      #include<stdio.h>
      #include<conio.h>
      void main()
      {
          FILE *fp,*f2;
          int i,n,x[100],num;
          int add,mul,y[10];
          int h[10]={5,2,3,6,1};
          clrscr();
          printf("contents of DATA file");
          fp=fopen("DATA.txt","w");
       
      /*writing integers as an array into "data" file*/
          for(i=0;i<100;i++)
          {
              n=i;
              x[n]=2*i;
              putw(x[n],fp);
          }
       
      /*printing the written data*/
          while ((num=fscanf(fp))!=EOF)
              printf("\n %d",num);
          fclose(fp);
          fp=fopen("DATA.txt","r");
          f2=fopen("result.txt","w");
       
      /*using the file "data" for further processing*/
          while((num=fscanf(fp))!=EOF)
          {
      /*logic for convolution*/
              for(n=0;n<=104;n++)
              {
                  add=0;
                  for(i=0;i<=n;i++)
                  {
                      mul=num*h[n-i];
                      add=mul+add;
                  }
                  y[n]=add;
                  putw (y[n],f2);
              }
          }
          fclose(fp);
          fclose(f2);
       
      /*printing the contents of file "result"*/
          f2=fopen("result.txt","r");
          while((num=fscanf(f2))!=EOF)
          printf("%d",num);
          fclose(f2);
      }
      i changed the files to txt and used fscanf() instead of getw() but still its not working. compiling the program gives error at fscanf(). with putw() its running but though the no. of integers required at the o/p is correct, the o/p itself is not the desired one. please help me.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        fscanf is for files in text mode and you have to write your file using separators that fscanf can deal with.

        If you use putw() that should write one binary integer to uout file. This would write sizef(int) bytes to your file. You should be able to read this integer by repositioning the file to the beginning and calling getw().

        Write a main() that does just that. Open a file, write an it using putw, reposition to the beginning of the file by calling
        seek(). Then call getw to an int variable and you should see your value.

        Once this works change the main() to write and retreive two ints.

        putw will write sizeof(int) bytes to your file and getw() will read sizeof(int) bytes. This allows the two int values to be adjacent in the file.

        However, fscanf(fp,"%d%d ", &var1, &var2)requir es the ints to be delimited, like separated by whitespace.

        If you use fscanf, you can also use fprintf and that will let you use the %f format flag to write floating point. Then and fscanf using %f can read that float.

        Comment

        Working...