943,796 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5074
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 11th, 2006
0

FILE + conversion from dec to hex = I need help

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 11th, 2006
0

Re: FILE + conversion from dec to hex = I need help

C or C++?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

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
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

C, not C++. Sorry, I should have said in the first place.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 12th, 2006
-1

Re: FILE + conversion from dec to hex = I need help

you can use fscanf("%d", ...) to read them into an int memory, and fprintf("%X" ...) to write them back out as hex.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char dek_hex(int); /*a function I'll explain later*/
  5. main(){
  6. FILE *a,*b;
  7. int broj;
  8. a=fopen("C:\\C_fajlovi\\deci.txt","r"); /*created a file in the specified directory, it's all cool at this point*/
  9. b=fopen("C:\\C_fajlovi\\hex.txt","w"); /*create a new file to write to*/
  10. while(fscanf(a,"%d",&broj)!=EOF)
  11. 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*/
  12. fcloseall();
  13. }
  14. 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

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.

  1. FILE* fp ;
  2. if( ( fp = fopen( "a.txt", "r" ) ) == NULL )
  3. {
  4. fputs( "file opening failed", stderr ) ;
  5. exit( 1 ) ;
  6. }

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

>char dek_hex(int m)

You do realise that would return a single char. Is that what you want.
Last edited by iamthwee; Nov 12th, 2006 at 2:06 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" . Example:
  1. char buf[20];
  2. int i = 16;
  3. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 12th, 2006
0

Re: FILE + conversion from dec to hex = I need help

like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X".
I'm not supposed to use any functions I'm not familiar with because I can't use them at my exam either. And my teacher never mentioned ssprintf, all I know out of that family is printf and fprintf.

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.
Yes, I'll do that. That was stupid of me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: program output
Next Thread in C Forum Timeline: Setting the Focus on a control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC