•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,707 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3431 | Replies: 17 | Solved
![]() |
•
•
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation:
Rep Power: 2
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation:
Rep Power: 36
Solved Threads: 882
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 Age is unimportant -- except in cheese
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation:
Rep Power: 36
Solved Threads: 882
you can use fscanf("%d", ...) to read them into an int memory, and fprintf("%X" ...) to write them back out as hex.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation:
Rep Power: 2
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 12:59 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation:
Rep Power: 36
Solved Threads: 882
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.
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
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.
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C Forum
- Previous Thread: program output
- Next Thread: Setting the Focus on a control



Linear Mode