GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Dec-2005, 11:35
edd_y_123 edd_y_123 is offline
New Member
 
Join Date: Dec 2005
Posts: 2
edd_y_123 is on a distinguished road

Need help writing a program that reads from a file and converts it to binary.


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
  #2  
Old 10-Dec-2005, 12:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help writing a program that reads from a file and converts it to binary.


Let's first assume you read the line into a char* array called inline. The "8" is at inline[3].

Q: Are you trying to convert it yourself to learn how or use a convert function like atoi()?


If atoi(), you pass the address of the first character to the function:
CPP / C++ / C Code:
num = atoi(&inline[3]);
The & says "pass the address of this value"

If you're trying to convert it yourself, you can set up a loop if you don't know how many characters are going to be converted. In your case if it's always 3 you can use:
CPP / C++ / C Code:
num = (inline[3] - 48) * 100;
num +=(inline[4] - 48) * 10;
num +=(inline[5] - 48);
The "- 48" converts the character "8" to the binary number 8.
The "+=" is shorthand for
CPP / C++ / C Code:
num = num + (inline[4] - 48) * 10;
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 10-Dec-2005, 20:08
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need help writing a program that reads from a file and converts it to binary.


Another way to do this:
There is a readymade function sscanf to scan a string for a specific format.

First, use fgets to read a string buffer from the file.
then scan the string for two different integers using sscanf.
Something like this:
CPP / C++ / C Code:

sscanf( buffer, "%d %d ", &num1, &num2);


Now, num2 is the required second number, say 801.

Then as you know, convert num2 to binary.

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 18-Dec-2005, 11:21
edd_y_123 edd_y_123 is offline
New Member
 
Join Date: Dec 2005
Posts: 2
edd_y_123 is on a distinguished road

Re: Need help writing a program that reads from a file and converts it to binary.


Aaah thanks guys!
Actually im just trying to learn how to do it myself, but I have been reding up on this atoi thing and it looks good as well.
Thanks again guys! When I get home today I will try to see what I can do.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling/Linking roostercogburn C Programming Language 5 08-Nov-2005 15:32
Vernam encryption cipher Zorachus C++ Forum 9 31-Jul-2005 15:49
Dynamic memory, loops, and segmentation faults ubergeek C++ Forum 2 07-May-2005 17:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.