473,657 Members | 2,592 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 1287
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
3198
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
7
3969
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 byte sequences. _I_ would read it into arrays of unsigned char and use C's memcmp(), but as you see Im a novice C++ programmer and think that theres some better, typically used, way. Regards
1
3261
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 structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then be read back and loaded. However I'm having difficulty with this -- I found out (due to an...
2
1622
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 act like database and it can be created using one of ADO.NET component. Is it true ? Thx Alot ... MARTIN -NEWBIE IN VB.NET-
4
4604
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> #include<conio.h> #include<stdlib.h>
1
2434
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 you
3
1862
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 command button. when we click cmd button once the data from the notepad should be read as strings of different sizes & displayed in the text boxes. Again when we click the cmd button the data in various text fields are written to another...
2
3684
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 character arrays) and extracting it fom the file. Might be possible there is a peoblem in my approach I am designing it with the help of OOP i.eclasses untill now i have made a structure inside the class. plz suggest me the way to implement them...
9
3242
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 information from there and want store in my own binary file with .vbf extension ( not like .dat file.) i am putting code here for reading text file and extract some information from there but how can i store that information in my own binary file. ...
1
1011
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 in which folder.
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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 we have to send another system
2
1736
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.