473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

code for file handling operation in C for window OS

2
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?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5.     FILE *fp,*f2;
  6.     int i,n,x[100],num;
  7.     int add,mul,y[10];
  8.     int h[10]={5,2,3,6,1};
  9.     clrscr();
  10.     printf("contents of DATA file");
  11.     fp=fopen("DATA.c","w");
  12.  
  13. /*writing integers as an array into "data" file*/
  14.     for(i=0;i<100;i++)
  15.     {
  16.         n=i;
  17.         x[n]=2*i;
  18.         putw(x[n],fp);
  19.     }
  20.  
  21. /*printing the written data*/
  22.     while ((num=getw(fp))!=EOF)
  23.         printf("%d",num);
  24.     fclose(fp
  25.     );
  26.     fp=fopen("DATA.c","r");
  27.     f2=fopen("result.c","w");
  28.  
  29. /*using the file "data" for further processing*/
  30.     while((num==getw(fp))!=EOF)
  31.     {
  32. /*logic for convolution*/
  33.         for(n=0;n<=104;n++)
  34.         {
  35.             add=0;
  36.             for(i=0;i<=n;i++)
  37.             {
  38.                 mul=num*h[n-i];
  39.                 add=mul+add;
  40.             }
  41.             y[n]=add;
  42.             putw (y[n],f2);
  43.         }
  44.     }
  45.     fclose(fp);
  46.     fclose(f2);
  47.  
  48. /*printing the contents of file "result"*/
  49.     f2=fopen("result.c","r");
  50.     while((num=getw(f2))!=EOF)
  51.     printf("%d",num);
  52.     fclose(f2);
  53. }
  54.  
Jan 29 '12 #1
3 2682
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 30 '12 #2
saya b
2
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.     FILE *fp,*f2;
  6.     int i,n,x[100],num;
  7.     int add,mul,y[10];
  8.     int h[10]={5,2,3,6,1};
  9.     clrscr();
  10.     printf("contents of DATA file");
  11.     fp=fopen("DATA.txt","w");
  12.  
  13. /*writing integers as an array into "data" file*/
  14.     for(i=0;i<100;i++)
  15.     {
  16.         n=i;
  17.         x[n]=2*i;
  18.         putw(x[n],fp);
  19.     }
  20.  
  21. /*printing the written data*/
  22.     while ((num=fscanf(fp))!=EOF)
  23.         printf("\n %d",num);
  24.     fclose(fp);
  25.     fp=fopen("DATA.txt","r");
  26.     f2=fopen("result.txt","w");
  27.  
  28. /*using the file "data" for further processing*/
  29.     while((num=fscanf(fp))!=EOF)
  30.     {
  31. /*logic for convolution*/
  32.         for(n=0;n<=104;n++)
  33.         {
  34.             add=0;
  35.             for(i=0;i<=n;i++)
  36.             {
  37.                 mul=num*h[n-i];
  38.                 add=mul+add;
  39.             }
  40.             y[n]=add;
  41.             putw (y[n],f2);
  42.         }
  43.     }
  44.     fclose(fp);
  45.     fclose(f2);
  46.  
  47. /*printing the contents of file "result"*/
  48.     f2=fopen("result.txt","r");
  49.     while((num=fscanf(f2))!=EOF)
  50.     printf("%d",num);
  51.     fclose(f2);
  52. }
  53.  
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.
Jan 30 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
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)requires 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.
Jan 31 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
11
by: Josh | last post by:
Hi, I am having a problem with Python. I am new to Python as a programming language, but I do have experience in other languages. I am experiencing strange problems with File handling and wonder...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
7
by: D & J G | last post by:
Is there a way to instruct Windows file-handling - 'Opens with:' - from within VB6? I want to ensure that files with the .rtf (rich text) extension always go to Word Pad, no matter which future...
3
by: Binu C | last post by:
hi am new to VB. I need to develop an application. The requirements are as follows: We have a notepad which consists of some data(Say A). Now we have a form in which we have some text fields & a...
5
by: fazulu deen | last post by:
HI all, I have to write a C program ,suppose if i want to tranfer 1MB from host memory to device say SD ,first i have to split it into 5kbytes after having splitted into 5kbytes each, not more...
9
by: lokeshrajoria | last post by:
hello everybody, i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull...
3
by: neha_chhatre | last post by:
hey please help me execute the code on ubuntu in C #include<stdio.h> #include<stdlib.h> int main() { FILE *fp; char ch; float ticktock;
6
Parul Bagadia
by: Parul Bagadia | last post by:
This is my first program of file handling... Here we are supposed to simulate sql language...... This is ofcourse not the complete program; m done only with insertion and select* thing; insert...
2
by: phpmagesh | last post by:
Hi, I want to create a xml document using php-file handling . that i have a Database named as Total_category and table name as students_one. and i have fields like name, age, email, title,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.