Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unwanted automatic carriage return using fprintf

Status
Not open for further replies.

nohairleft

Programmer
Feb 2, 2005
3
US
I am using Visual C++ version 6.0. I have a C console program that needs to output x-y coordinate information in binary. When I output a one byte value of 10 (0A Hex) the output file contains two bytes (0D and 0A). In ASCII the 0A would be defined as a line feed and 0D as a carriage return. I am forcing the data to be contained within a single byte by using the instruction fprintf(outfile, "%c", (coordinate & 0xFF)), where coordinate is defined as an int. This method works fine (no 0D is output) with the Borland compiler and also with GNU C on my LINUX box but the Microsoft compiler is 'seeing' the line feed and automatically outputting a carriage return with it. Is this a compiler option? Does anyone know how to surpress this unwanted carriage return from being written to my output file?
 
Open the file in binary mode

Code:
FILE *fp = fopen("file.bin","wb");

This is better than fprintf, especially if you're just writing a single character.
Code:
fputc( (coordinate & 0xFF), fp );
I'm not sure that fprintf() could cope with outputting the '\0' character, since it is actually supposed to be a text function.

--
 
Thanks. That seemed simple enough. I'm pretty old school and was not aware of more than "r", "w", and "a". Just a side note, my file pointer was defined globally and this caused the linker to blow up and not be able to create the .exe file. I used the fopen function as you show, with the file pointer defined on the same line, and the linker was very happy. And so am I.
 
There is a very strange statement about my file pointer was defined globally and this caused the linker to blow up and not be able to create the .exe file.
Can you explain?..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top