473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding file handling and conversion of data

2 New Member
sir
i am having a 1file (given file) and in that i have some binary information and i niw i want to convert those binary information into decimal form and save it into new file (output fle)
i was written a code but that was not working properlyplease help me out
thank you

mycode
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3. int i,j,n,ch;
  4. int a[100][100];
  5. FILE *p,*q;
  6. char file1[20],file2[20];
  7.  
  8. printf("enter the given file name");
  9. gets(file1);
  10. p=fopen(file1,"r");
  11. if(p==NULL)
  12. {
  13. printf("cant open",file1);
  14. exit(0);
  15. }
  16.  
  17. printf("enter output file name");
  18. gets(file2);
  19. q=fopen(file2,"w");
  20. if(q==NULL)
  21. {
  22. printf("cant open",file2);
  23. exit(0);
  24. }
  25.  
  26. for (i=0;i<n;i++)
  27. {
  28. for (j=2;j>=0;j--)
  29. {
  30. fscanf(p,"%d",ch);
  31. a[i][j]=a[i][j]+a[i][j]*pow(j,j)*ch;
  32. }
  33. fputc(a[i][j],q);
  34. }
  35.  
  36. if(a[i][j]==EOF)
  37. {
  38. fclose(p);
  39. fclose(q);
  40. return(0);
  41. }
  42. }
Jan 10 '14 #1
2 1275
Banfa
9,065 Recognized Expert Moderator Expert
General: Decimal form is not a well recognised description of data. There is binary, raw data in memory, and text, something that is human readable. If using text you may want to indicate what base to display the number in but decimal would be assumed if you left it out since decimal is for humans to read and decimal is the way they normally read numbers. You seem to be saying that you want to read a file that contains binary, non-human readable, data and write it to a file that contains text, human readable, data using base 10. However that is the reverse of what your program appears to attempt to do so I am a little confused.

General: properly indenting your code would make it more readable.

Line 1: main returns int

Line 4: int a[100][100]; is logically incorrect. You are reading a list of integers not a matrix of integers. Further down, at line 31, you read the separate bytes of each integer into different entries in a without ever combining them.

Line 9/18: gets is not safe especially when you have only provided 20 byte buffers to contain the file name.

Line 26: n is never initialised to anything, it has a random value.

Line 30: You are using fscanf to read a file that you have said is binary, fscanf is for reading text data. At line 31 you then attempt some strange maths which suggests you may think you are trying to read binary data. To read binary data either use fgetc or use fread.

Line 31: This looks like you may be trying to recombine binary bytes into an integer. You have the logical wrong. You only ever modify a[i][j] based on its current value, however you never initialise the current value anywhere you need to set a[i][j] to 0 somewhere. You also use the value of a[i][j] as a multiplier of the value read which is not correct and pow(j,j) raise j to itself seems wrong did you mean pow(2,j) also pow uses type double which makes it unreliable in an integer operation. It is more standard when recombining an int to use the shift operators since they work on ints i.e. a[i][j] += ch << (j * 8);

Line 33: You try to write the text value to file but using fputc you truncate the int a[i][j]. Also at this point j is -1 and you are accessing the array outside it's boundaries, which is definitely undefined behaviour and probably a crash.

Line 36: You only close the files and call exit if a[i][j] is EOF. EOF should be the end condition for your loop at line 26. At the end of the program you always need to close your files and you should return a value from main.


PSEUDO CODE

Read a binary 4 byte integer from a file where it is stored in big endian format

Expand|Select|Wrap|Line Numbers
  1. REPEAT
  2.   result = 0;
  3.  
  4.   FOR ix IS 0 TO 3
  5.       value = READ BYTE FROM FILE EXIT IF FAILED
  6.  
  7.       result |= value << ((3 - ix) * 8)
  8.   ENDFOR
  9.  
  10.   USE result
  11.  
  12. UNTIL EOF
  13.  
Write a binary 4 byte integer to a file where it is stored in big endian format

Expand|Select|Wrap|Line Numbers
  1. REPEAT
  2.   result = GET NEXT INTEGER;
  3.  
  4.   FOR ix IS 0 TO 3
  5.       value = (result >> ((3 - ix) * 8)) & 0xFF
  6.  
  7.       WRITE BYTE value TO FILE EXIT IF FAILED
  8.   ENDFOR
  9.  
  10.   USE result
  11.  
  12. UNTIL NO MORE INTEGERS
  13.  
Jan 10 '14 #2
donbock
2,426 Recognized Expert Top Contributor
------------------------------
Jan 10 '14 #3

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

Similar topics

9
3188
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...
7
3947
by: Matthias Czapla | last post by:
Hi! Whats the canonical way for handling raw data. I want to read a file without making any assumption about its structure and store portions of it in memory and compare ranges with constant...
1
3247
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...
2
1610
by: Martin | last post by:
Dear All VB.NET programmers, Does anyone of you have a good article reference to learn file handling (text, binary, etc) and XML (creating, reading, etc) in VB.NET. I heard that XML file may...
4
4601
by: BHARAT | last post by:
Hi I am a little bit new to C's advanced topics: I need help in file handling. I have Turbo C and I wrote a simple program to write data to file: #include<stdio.h> #include<dir.h>...
1
2425
by: ivertobson | last post by:
how can i read all the datas stored in a text file into an array of structures? and the memoryfor the array of structure should be allocated dynamically.please give me teach me the coding ......thank...
3
1846
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...
2
3676
by: coolprateek2007 | last post by:
Here will designing the code for the library management software i am facing the problem while handling the data of the variables I am not able to insert the data into the file(specifically of the...
9
3235
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...
1
1000
by: ndv472 | last post by:
sir when we use "fopen" then we have to specify a file name so after writing a program to open any file so where we have to save that file (location of file) so that it will open in which drive and...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.