4 Replies - 228 Views - Last Post: 10 February 2014 - 07:39 PM Rate Topic: -----

#1 fishpond  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 14-December 13

Printing Out Unicode Hex as Chars

Posted 10 February 2014 - 07:01 PM

Hi guys,

I'm writing a school assignment that writes/reads user input into and out of a binary file.

I've gotten the write part to work, but now I need to be able to read that file back in and display it as a string.


I think I should be using fread() and read my file into an array of int's right? But when I try printing out that array I get a whole bunch of numbers that don't match the hex code in my file.

How do I read in a binary file and print it out as a string?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Printing Out Unicode Hex as Chars

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Printing Out Unicode Hex as Chars

Posted 10 February 2014 - 07:04 PM

Quote

How do I read in a binary file and print it out as a string?

That really depends on how you wrote the file. Post your code for both the read and write of the file then maybe we can help.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 fishpond  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 14-December 13

Re: Printing Out Unicode Hex as Chars

Posted 10 February 2014 - 07:16 PM

View Postjimblumberg, on 10 February 2014 - 07:04 PM, said:

Quote

How do I read in a binary file and print it out as a string?

That really depends on how you wrote the file. Post your code for both the read and write of the file then maybe we can help.

Jim


The program basically asks for 3 inputs. Last name, first name, and light sabre power.

I've actually gotten it to print out the last name, but not the first name...

int DataEntryMode( FILE *stream )
{
	int lastNameComplete;
	int firstNameComplete;
	int saberPowerComplete;

	int size;
	char * buffer;
	size_t result;

	char lastName[17] = "";
	char firstName[17] = "";
	float sabrePower = 0;

	do
	{
		lastNameComplete = ReadInLastName( lastName );
	}
	while(lastNameComplete);

	do
	{
		firstNameComplete = ReadInFirstName( firstName );
	}
	while(firstNameComplete);
	
	do
	{
		saberPowerComplete = ReadInSaberPower( sabrePower );
	}
	while(saberPowerComplete);
	
	fwrite(lastName, sizeof(lastName[0]), sizeof(lastName), stream );
	fwrite(firstName, sizeof(firstName[0]), sizeof(firstName), stream );

	/* obtain file size: */
	fseek (stream , 0 , SEEK_END);
	size = ftell (stream);
	rewind (stream);

	/* allocate memory to contain the whole file: */
	buffer = (char*) malloc (sizeof(char)*size);
	if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

	/* copy the file into the buffer: */
	result = fread (buffer,1,size,stream);
 	if (result != size) {fputs ("Reading error",stderr); exit (3);}

 	printf("%s", buffer);

	return 1;
}

void ClearStdinBuffer( void )
{
	int c;

	while((c = getchar()) != '\n' && ( c != EOF ) );

	return;
}

int ReadInLastName( char *lastName )
{
	printf( "Enter Last Name: " );
	fgets( lastName, 16, stdin );
	ClearStdinBuffer();

	return 0;
}
int ReadInFirstName( char *firstName )
{
	printf( "Enter First Name: " );
	fgets( firstName, 16, stdin );
	ClearStdinBuffer();

	return 0;
}
int ReadInSaberPower( float power )
{	
	char sabrePowerString[5] = "0.00";

	printf( "Enter Saber Power: " );
	fgets( sabrePowerString, 5, stdin );
	ClearStdinBuffer();

	/* sscanf returns 0 or less if there was an error */
	if( sscanf( sabrePowerString, "%f", &power ) <= 0 ) return 1;

	return 0;
}




I'm really new to C, so if you see anything I'm doing wrong, please let me know.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Printing Out Unicode Hex as Chars

Posted 10 February 2014 - 07:25 PM

Please post the smallest possible complete program that illustrates your problem. One which can be compiled and ran so we can see the problem for ourselves.

Edit: One thing I do see is that you are not trying to read exactly what you wrote. You wrote two variables, you must read two variables, and these variables must be of the same sizes and types.


Jim

This post has been edited by jimblumberg: 10 February 2014 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Printing Out Unicode Hex as Chars

Posted 10 February 2014 - 07:39 PM

That printf() on line 49 will not work because remember that the %s string formatter stops printing out when it encounters a null terminator. Note that you wrote out the null terminator as part of your output file with your code on lines 33 and 34. So when you read the data back in as a single buffer on line 46, the null terminator is present.

Now with regards to your title: You mentioned trying to print out "Unicode hex as chars". So far I've not seen any code yet dealing with Unicode, so I don't know exactly what you are trying to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1