| | |
FILE + conversion from dec to hex = I need help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
I'm pretty much new to using files, so I'm not sure if this is a banale question.
Here's the assignment: "Each line of the file dec.txt contains a whole number (>0). Write a program that creates a file called hex.txt. Each line of this file is to contain a string representing the hexadecimal equivalent of the whole number in the corresponding line of dec.txt."
Can someone please walk me through this?
Here's the assignment: "Each line of the file dec.txt contains a whole number (>0). Write a program that creates a file called hex.txt. Each line of this file is to contain a string representing the hexadecimal equivalent of the whole number in the corresponding line of dec.txt."
Can someone please walk me through this?
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
This will load the hex values from smallest to largest:
make a character array (hexval) about 10 characters long set whole array to spaces set hexval[9] to '\0' set hexptr to 8 -- this will point into hexval to load the hex chars. Read a line into an integer (intval) -- that's base 10. Loop until that integer is 0: set hexval[hexptr] = remainder of intval/16 add 48 ('0') to hexval[hexptr] -- this converts the integer value to a digit character if hexval[hexptr] > 57 ('9') add 7 -- value is now 'A' - 'F' decrement hexptr -- to load the next hex digit divide 16 from intval -- to remove the hex val we just loaded End of loop
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
Yes, it's fairly obvious that can be used. Iguess my question wasn't very to-the-point. Here's what I got so far, and what I need from you:
So my questions would be:
1. is my general idea alright?
2. what should my function look like?
3. can it be done without a function?
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> char dek_hex(int); /*a function I'll explain later*/ main(){ FILE *a,*b; int broj; a=fopen("C:\\C_fajlovi\\deci.txt","r"); /*created a file in the specified directory, it's all cool at this point*/ b=fopen("C:\\C_fajlovi\\hex.txt","w"); /*create a new file to write to*/ while(fscanf(a,"%d",&broj)!=EOF) fprintf(b,"%s",&dek_hex(broj)); /*dek_hex should be a function that turns a dec number into hexadecimal and returns it in the form of a string*/ fcloseall(); } char dek_hex(int m){ /*this is where my problems start. No matter how I write the function, I keep getting all kinds of error messages*/
So my questions would be:
1. is my general idea alright?
2. what should my function look like?
3. can it be done without a function?
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Okay here are a few pointers:
1. Use int main( ) not just main( ) and append a return 0 stmt at the end of main( ).
2. Dont hard code the file names in the function, its not a good practice. Just keep the file name in a constant array of characters.
3. I am not sure on this one, but, dont use \\ in your file paths, it is not cross platform complaint. Use forward slashes instead ( / ).
4. After opening the files do check whether the operation is successful, it is a good practice.
5. fscanf( ) normally returns the number of items scanned and in the case of error a NULL is returned.
1. Use int main( ) not just main( ) and append a return 0 stmt at the end of main( ).
2. Dont hard code the file names in the function, its not a good practice. Just keep the file name in a constant array of characters.
3. I am not sure on this one, but, dont use \\ in your file paths, it is not cross platform complaint. Use forward slashes instead ( / ).
4. After opening the files do check whether the operation is successful, it is a good practice.
c Syntax (Toggle Plain Text)
FILE* fp ; if( ( fp = fopen( "a.txt", "r" ) ) == NULL ) { fputs( "file opening failed", stderr ) ; exit( 1 ) ; }
5. fscanf( ) normally returns the number of items scanned and in the case of error a NULL is returned.
Last edited by ~s.o.s~; Nov 12th, 2006 at 1:59 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" . Example:
If you use that in dek_hex() you will have to change the return value from char to char* because as you can see from the above code snippet the hex value is an array of characters, not a single character value.
C Syntax (Toggle Plain Text)
char buf[20]; int i = 16; sprintf(buf,"%x",i);
If you use that in dek_hex() you will have to change the return value from char to char* because as you can see from the above code snippet the hex value is an array of characters, not a single character value.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
•
•
•
•
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X".
Yes, I'll do that. That was stupid of me.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
vampires that stalk Earth on reverse hours - Jus Allah
And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
![]() |
Other Threads in the C Forum
- Previous Thread: program output
- Next Thread: Setting the Focus on a control
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






