Advertisement

getc() being screwy

Started by December 31, 2002 04:20 AM
2 comments, last by HotTruBlu 21 years, 10 months ago
Hey i''m working on a program that switches every 2 bytes in a file (ex. 01 02 03 04 becomes 02 01 04 03) and I''m using getc(file) to read bytes out of the original file. I''ve had some probs with it when it runs into nulls, but that''s no longer a problem; I just use fseek before it to make sure it moves to the next byte. Anyway here''s my main reading/writing loop: for (int load=1; load < file_size; load+=2) { fseek(file,load,SEEK_SET); data[0]=getc(file); fwrite(data, 1, 1, wFile); fseek(file,load-1,SEEK_SET); data[0]=getc(file); fwrite(data, 1, 1, wFile); } The reason I''m messing with data is cause I originally wrote the file loading function to load a file into ram, so it returns a pointer to an unsigned char, aka data, and I was just too lazy to change it cause it''s almost 2 AM now and i felt like putting data to use. data is defined like this: unsigned char *data = (unsigned char *)malloc(1); Anyway I know this is getting kinda long but the problem is that whenever there is the byte 0x0A, 0x0D keeps popping in front of it. I''m aware that 0D 0A are the two bytes signaling a return and a new line, and i''m thinking that might have something to do with it? Anyway I''m assuming getc() is the source of the problem, maybe it''s not, but please try to help me out here.
You need to open your file in binary mode :
FILE* file = fopen( "infile", "rb" );FILE* wFile = fopen( "outfile", "wb" ); 


otherwise, '\n' (0x0A, newline) gets translated to "\r\n" (0x0D 0x0A, carriage return, newline) on DOS/Windows platforms (other platforms translate differently).

And why don't you just use fread() instead of getc() (or even fgetc() ) ? You should also do error checking (ferror()).

I also wonder why you're dynamically allocating a 1-byte buffer.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on December 31, 2002 5:41:15 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Hey thanks it works now, i didn''t simply because i have an extremely limited vocabulary of functions; i''ve only been really using c++ for the past couple weeks, and most of what i have learned has been from my friend. Recently i''ve been pretty busy too, so i haven''t had much time to look up on different functions. Anyway, thanx again.
These are C functions

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement