![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
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
|
||||
|
||||
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:
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:
The "+=" is shorthand for CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
||||
|
||||
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:
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
|
|||
|
|||
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 GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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