LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (c) int's and char's (https://www.linuxquestions.org/questions/programming-9/c-ints-and-chars-596615/)

brazilnut 11-02-2007 09:47 AM

(c) int's and char's
 
Converting between the two I seem to get errogenous data, but it could be elsewhere in the pipeline, here's code (sorry about length):
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int binreader(char * fname)
{
        FILE *fi;
        char ch;
        if ((fi = fopen(fname, "rb")) != NULL)
        {
                printf("\nBIN READER\n");
               
                int i = 0;
                while ((ch = getc(fi))!= EOF)
                {
                      printf("%d: %d : %c \n", i++, (unsigned int)ch, ch);
              }
              fclose(fi);
    }
       
        printf("\n");
        return 0;
}

unsigned int gen_rand()
{
        while(1)
        {
                unsigned int n = abs(rand() % 255);
                if(n != EOF)
                {
                        return n;
                }
        }
}

int gen_random_file(char * fname, int size)
{
        FILE *fo;        int i;
        if ((fo = fopen(fname, "wb")) != NULL)
        {
                for(i=0;i<size;i++)
                {
                        unsigned int ic = gen_rand();
                        char cc = (char)ic;
                        putc(cc, fo);
                        printf("%c: %d \n", cc, ic);
                }
                fclose(fo);
        }
}

int main(int argc, char *argv[])
{
        char * fname = "key.txt";
        int iret = gen_random_file(fname, 10);
        iret = binreader(fname);
        return 0;
}


brazilnut 11-02-2007 09:59 AM

OK, i've changed the following line in the 'binreader()' function and now it looks OK, but maybe someone has some other ways...
Code:

printf("%d: %d : %c \n", i++, (unsigned int)((unsigned char)ch), ch);
When I tried doing this with the 'while ((ch = getc(fi))!= EOF)' line, it tripped saying unable to reinterpret EOF, therefore, how to read in unsigned char's?

bigearsbilly 11-02-2007 10:09 AM

er, FYI,
;)

erogenous
erroneous

brazilnut 11-02-2007 10:12 AM

Yes, but I still think my versions' better, I also never get depreciated and deprecated right. (Oh I just 'av!)

bigearsbilly 11-02-2007 10:44 AM

erogenous data would be stuff on those naughty web sites

brazilnut 11-02-2007 11:05 AM

Or as my little nephew would say, decapitated!

ntubski 11-02-2007 12:54 PM

Quote:

Originally Posted by brazilnut (Post 2945774)
OK, i've changed the following line in the 'binreader()' function and now it looks OK, but maybe someone has some other ways...
Code:

printf("%d: %d : %c \n", i++, (unsigned int)((unsigned char)ch), ch);
When I tried doing this with the 'while ((ch = getc(fi))!= EOF)' line, it tripped saying unable to reinterpret EOF, therefore, how to read in unsigned char's?

getc() returns an int. EOF is usually define as -1, when you cast the unsigned char 255 into signed char (which is what happens you assign to ch) you will also get -1, so the code won't work as expected in all circumstances (see 2's complement). Basically, declare ch an int should fix things.

Also testing the return value of rand() against EOF doesn't make much sense, nor does taking the absolute value of it since it returns a positive number anyway. You never return a value from gen_random_file() even though you declared it to return an int.

brazilnut 11-02-2007 01:02 PM

Yes most of those were put in for testing and were still there until you mentioned... Cheers!

P.S. And yes, getc does return an int, the example I used was a char, but ref does show it's an int... lollipop

David1357 11-02-2007 01:55 PM

getc returns an int
 
Your first problem is a basic problem covered in many intro to programming books: You are storing the result of getc (which returns an int) in a char type. EOF is -1, so when you compare "ch" to "EOF" they will be equal if you see any bytes in the file that have a value of 255, as this sample code demonstrates:

Code:


#include <stdio.h>
#include <stdlib.h>

void main(void)
{
    char ch;

    ch = EOF;
    printf("ch = %c, %d, 0x%08X\n", ch, (int) ch, (int) ch);

    ch = 255;
    printf("ch = %c, %d, 0x%08X\n", ch, (int) ch, (int) ch);

}

The output of this code is the following:

Code:


ch =  , -1, 0xFFFFFFFF
ch =  , -1, 0xFFFFFFFF

Note that 255 is not a printable character on most systems.

brazilnut 11-02-2007 02:09 PM

What's the unused for '0', because when I read from file it it's acting like an EOF. (See other post 'One Time')

I've changed all usage to fgetc etc and all appropriate chars to ints, but it exit's the loop...

I believe it's generally -1 elsewise 255 if unsigned, supposedly defined in stdio.h. However i'm of to try using feof() instead... see if that solves my little problem!


All times are GMT -5. The time now is 11:22 AM.