Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help writing a program that reads from a file & converts it to binary.

Status
Not open for further replies.

Eddy123

Programmer
Dec 10, 2005
3
US
Hello folks. I am trying to teach myself C and am having little luck so far. I can follow the books examples fairly well but when it comes time to write my own program, I get stuck.
The book has me write a little self-test program but offers no help whatsoever. The program is supposed to read the following from a .txt file (ASCII format):

01 801
02 901
03 802
04 902
05 803
06 903
07 804
08 904
09 805
10 905

It is then supposed to convert only the second set of numbers to binary! (Exclude the first, like 01-10).
EX: Instead of converting 01 801 to binary, it is only supposed to convert 801!

Does anyone have any idea on how to do this? Any help is appreciated as I am completely stuck and since the book does not have a coded example I don’t even know where to start. I can skip this example but I do not wish to because I actually want to know how to do this.
If anyone has coded something like this before, can you please post your code? If I can’t do it, I would at least like to see what it looks like.

Thanks,


Eddy
 
Post what you've tried so far and please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

The first step is simply to print out what you thought you read in (before trying to convert anything).

--
 
Salem is right here,

Inch by inch, everything's a cinch.
Yard by yard, you're making it hard.

First write a little routine that simply reads each record and prints out what it's read.
Next you can look at maybe "sscanf" for processing the records.
This is really a very simple thing to do but I realise that it might seem really difficult when you're starting out.
Give it a go, get as far as you can and post what you have when you get stuck and we'll help you further.
You'll learn the most if you try to do some of this yourself.
If we do it all for you you'll learn little or nothing.

Have faith in yourself.

Good luck.


Trojan.
 
BTW: It's useful to have the manual pages for the standard libraries available to help you understand how to use stuff like "sscanf".
If you're using Unix or Linux you'll have the man pages available but if not this might help:


Trojan.
 
Thanks for the link!
When I get home later today I will post what I have. As it is right now I am posting on every c board I can find to see if I get a response, heh.
I have absolutely no idea how to convert things into binary, and am just starting to program in C… So this program seems to be way over my head. What I have is not much, but I will post it.
 
Hello all, this is what I have so far:

Using the
01 801
02 901
.. ...
etc etc
as my readfile


Code:
#include <stdio.h>

int main ()
{

FILE *fp, *fp1;
int filevar;
int linenumber = 0;


  fp = fopen("readfile.txt","r");
  fp1 = fopen("outfile.txt","w");

while(EOF != fscanf(fp,"%d",&filevar))
 {
  linenumber++;
  fprintf(fp1, "The value of line number %d was: %d\n",linenumber,filevar);
 }

fclose(fp);
fclose(fp1);
return 0;
}


What I am having it do, is read out of one file and copy the values of that file into another. However I am running into problems.
1. I do not know how to convert the second value to binary. (801, 901, etc)
2. For the output file it gives me 20 lines.
(EX:
The value of line number 1 was: 1
The value of line number 2 was: 801
The value of line number 3 was: 2
The value of line number 4 was: 901
End EX)

What I need it to do is ignore the 1,2, etc and only output the binary value of 801, 901, etc.
So the outfile should be 10 lines instead of 20... Giving me binary values for 801, 901, etc).

Any help on this is appreciated. I am bashing my head against the keyboard as we speak, heh.
 
Ok dude,

Firstly, process the whole record at once.
If you're gonna use fscanf then get two integers and not one.
Next issue, what do you mean by converting to binary?
Do you want a textual version of the binary value?
If so, you need to consider how to find each bit in turn.
The usual way is to see if the number is odd or even. If it's odd, the bit you need is a 1 otherwise it's a zero.
Then divide by 2 and repeat until the value itself is reduced to zero.
The only problem with this is that the binary will then be output the wrong way round.
For example, lets take the value 12 and process that.
is 12 odd? no = 0
halve gives 6
is 6 odd? no = 0
halve gives 3
is 3 odd? yes = 1
havle gives 1 (ignore fraction)
is 1 odd? yes = 1
halve gives 0 so finished.
As you can see we get 0011 whereas the result should be 1100 so your next issue is to reverse the sequence.
I'll leave you with those ideas for a while and see how much further you get.
BTW: To see if 'value' is odd, use either "value % 2" or "value & 1" and see if the result is zero (even) or 1 (odd).



Trojan.
 
> Look round these sites for envelopes which might be ok.
So read pairs of numbers in one call, like
fscanf ( fp, "%d %d", &linenum, &value )

> I do not know how to convert the second value to binary. (801, 901, etc)
Consider this sequence in base 10
801 % 10 = 1
801 / 10 = 80 (integer division truncates)
80 % 10 = 0
80 / 10 = 8
8 % 10 = 8
8 / 10 = 0 (all done)

Binary is just the same, except using 2 as the base.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top