| | |
FILE + conversion from dec to hex = I need help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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?
C Syntax (Toggle Plain Text)
while (fscanf(a, "%d", &broj) != EOF)
C Syntax (Toggle Plain Text)
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" .
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
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 4: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
>>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:
C Syntax (Toggle Plain Text)
while (fscanf(c,"%d",&a) > 0){
Last edited by Ancient Dragon; Nov 13th, 2006 at 4:28 pm.
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
•
•
•
•
>>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 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 12:03 pm.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
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.
C Syntax (Toggle Plain Text)
#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
C Syntax (Toggle Plain Text)
1 2 40 abc 5a
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
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
![]() |
Other Threads in the C Forum
- Previous Thread: program output
- Next Thread: Setting the Focus on a control
Views: 4571 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






