LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2007, 09:47 AM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
(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;
}
 
Old 11-02-2007, 09:59 AM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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?

Last edited by brazilnut; 11-02-2007 at 10:01 AM.
 
Old 11-02-2007, 10:09 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
er, FYI,


erogenous
erroneous
 
Old 11-02-2007, 10:12 AM   #4
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
Yes, but I still think my versions' better, I also never get depreciated and deprecated right. (Oh I just 'av!)
 
Old 11-02-2007, 10:44 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
erogenous data would be stuff on those naughty web sites
 
Old 11-02-2007, 11:05 AM   #6
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
Or as my little nephew would say, decapitated!
 
Old 11-02-2007, 12:54 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by brazilnut View Post
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.
 
Old 11-02-2007, 01:02 PM   #8
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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
 
Old 11-02-2007, 01:55 PM   #9
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
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.
 
Old 11-02-2007, 02:09 PM   #10
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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!
 
  


Reply

Tags
basic, char, int, programming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: Problem with char's in strings. RHLinuxGUY Programming 3 09-11-2006 12:36 AM
Networking&Text files; A string or two, and LOTS of int's quentusrex Programming 9 10-27-2004 05:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration