•
•
•
•
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,962 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,631 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: 3432 | Replies: 17 | Solved
![]() |
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,721
Reputation:
Rep Power: 36
Solved Threads: 884
Yes. But you should pass in the hex array. C cannot return a character array. Also, format your code. Indentation is extremely important at all times. So is spacing between terms, like:
Much easier to read.
Unless you post something you've tried, we have nothing to help with.
Basic idea in my previous post.
Yes it can. Considerations:
function: Modularizes the program, easier to reuse the function in another program later. Somewhat easier to debug.
non-function: Not passing the hex array is a minor benefit.
Wouldn't this nullify the purpose of understanding how decimal to hex works?
while (fscanf(a, "%d", &broj) != EOF)
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 */Basic idea in my previous post.
Yes it can. Considerations:
function: Modularizes the program, easier to reuse the function in another program later. Somewhat easier to debug.
non-function: Not passing the hex array is a minor benefit.
•
•
•
•
Originally Posted by Ancient Dragon
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" .
Age is unimportant -- except in cheese
•
•
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation:
Rep Power: 2
Solved Threads: 0
OK, nevermind, I figured it out. I threw out the function and just did the whole conversion from dec to hex in the body of 'fscanf' with local variables. I realized using a function was a completely pointless complication.
If someone has any suggestions they're still welcome.
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<stdlib.h> void main(){ FILE *c,*b; int a; c=fopen("C:\\C_fajlovi\\dec.txt","r"); b=fopen("C:\\C_fajlovi\\hex.txt","w"); if (c==NULL){ puts("Error!"); exit(1); } if (b==NULL){ puts("Error!"); exit(1); } while (fscanf(c,"%d",&a)!=EOF){ char s[30],temp; int i=0; if (a==0) s[i++]='0'; while (a>0){ /*turning a dec (int) to a hex (string) value*/ if ((a%16)<=9) s[i++]=(a%16)+'0'; else if ((a%16)==10) s[i++]='A'; else if ((a%16)==11) s[i++]='B'; else if ((a%16)==12) s[i++]='C'; else if ((a%16)==13) s[i++]='D'; else if ((a%16)==14) s[i++]='E'; else s[i++]='F'; a/=16; } s[i]='\0'; /*I was missing this part the last time, which is one of the reasons the whole program was acting crazy*/ for (i=0;i<strlen(s)/2;i++){ temp=s[i]; /*got a reversed string, turning it around*/ s[i]=s[strlen(s)-i-1]; s[strlen(s)-i-1]=temp; } fprintf(b,"%s\n",s); } }
If someone has any suggestions they're still welcome.
Last edited by Sin-da-cat : Nov 13th, 2006 at 3:20 pm.
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,721
Reputation:
Rep Power: 36
Solved Threads: 884
>>while (fscanf(c,"%d",&a)!=EOF){
fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error. What you want is this:
fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error. What you want is this:
while (fscanf(c,"%d",&a) > 0){ Last edited by Ancient Dragon : Nov 13th, 2006 at 3:28 pm.
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
•
•
•
•
>>while (fscanf(c,"%d",&a)!=EOF){
fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error.
My thought: the while(fscanf... loop would go through the file line by line untill it gets to EOF, at which point it will stop. It works just fine in every program I write (including the one I posted), never had a problem with that.
What will fscanf return when it gets to the end? What you suggested works too, so I suppose it returns 0, but how come my version always works, then?
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
Because EOF is actually a maro which is defined in
In your case its the end of stream thing which is causing the break of loop.
In Mr. Dragons case it is the 0 which is returned by the fgets( ) which is causing the termination.
stdio.h as #define EOF <integer constant expression < 0>•
•
•
•
The macro yields the return value used to signal the end of a stream or to report an error condition
In Mr. Dragons case it is the 0 which is returned by the fgets( ) which is causing the termination.
Last edited by ~s.o.s~ : Nov 14th, 2006 at 11:03 am.
"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,721
Reputation:
Rep Power: 36
Solved Threads: 884
Here is an example that will illustrate the difference between using EOF and 0. In this example, checking for EOF causes infinite loop because end-of-file is never reached due to conversion error. If you uncomment the while statement that check for 0 then the program will stop when conversion error occurs.
When you wrote your programs for school you probably wrote sample data files that did not contain errors. That will not always be the case in real-world.
This is sample data file
When you wrote your programs for school you probably wrote sample data files that did not contain errors. That will not always be the case in real-world.
#include <stdlib.h>
#include <stdio.h>
int main(int ac, char** av)
{
int a;
int counter = 0;
FILE* fp = fopen("myfile.txt","r");
if(fp != NULL)
{
while( fscanf(fp,"%d",&a) != EOF)
// while( fscanf(fp,"%d",&a) > 0)
{
printf("%d\n",counter++);
}
fclose(fp);
}
return 0;
}This is sample data file
1 2 40 abc 5a
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
Ah, I see what you mean. Had to put a 'getchar()' after 'fclose' though so the black screen would stay open more than a milisecond.
Thanks for the answers.
Thanks for the answers.
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