c++ - Random bytes with fread -


#post

My variable names are not impossible! This code will be removed when it works!

#post

OK, so I'm using Fred in stdio.h to read a text file. The problem is that I keep reading random bytes which are not present in the text file with my knowledge. I am assuming that they are part of the file scheme but I have to make sure that this is not my code.

  #include "stdafx.h" #ifdef WIN32 #include & lt; Io.h & gt; #else #include & lt; Sys / io.h & gt; #endif #include & lt; Fcntl.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Sys / stat.h & gt; # Include & lt; Stdio.h & gt; #include "n_script_timer.h" // using namespace std; #ifdef _INC_WCHAR typed wchar_t CHR; #Else Tidefe four CRR; #endif int _tmain (int argc, CHR * argv []) {#ifndef _DEBUG if (argc == 1) {printf ("You must drag a file to this program to run it."); Scanf ("% * c"); Return 0; } CHR * fname = argv [1]; # ALSSE #IfDif _INC_WCHAR CONST CRR FNM [16] = L "F: \\ Dilememe.beon"; #else const CHR fname [16] = "f: \\ deleteme.bin"; #endif #endif FILE * Infile; since a long time; Strip Scriptimer a; // static constant int bise = 40 9 6 * 6; Static const int bsize = 84; Typedef CHR Chhh [bsize]; Int Alan; Printf ("# opening file '% s' ... \ n", fname); #ifdef _INC_WCHAR (iffile = _wfopen (fname, l "rb")) == faucet = # (iffile = fopen (fname, "r") == faucet) #endif {printf ("error opening file ' % S '", fname); return 0;} Fseek (inFile, SEEK_SET, 0); #ifdef _WIN32 len = _filelength (infile-> _file); #lace lean = _file lamp (infile- & gt; _filino) ; #endif printf ("FileLength:% d \ n", lane); Printf ("# buffer ... \ n"); if ((float) lane / (float) bsize) / Bsize)} {alen = (len / bsize) + 1;} Allen = (lane / bisis); #ifdef WIN32 // sh * f * = new sh [allen]; chhha chah [allan]; # bell six chi [ Allen]; #endif printf ("# reading file ... \ n"); Start_ST (& amp; one); for Int i = 0; (i = 0; i & l T; alen; ++ i) Feeds (and ch [i], size (CHR), BSiAge, infile); printf ("[% i]% s", i, ch [i]);} endange (and Printf ("frames per millisecond:% f \ n", (float) lane / a.milliseconds); printf ("characters per second:% f \ n", (float); fclose (INFILE) Lane / a.milliseconds) * 1000;) scanf ("% * c"); return 0;}  

Here are some strange things:

  int i = 0; For (i = 0; i & lt; alen; ++ i) {feed (and ch [i], size (CHR), BSiAge, infix); Printf ("[% i]% s", i, ch [i]);  
  1. You do not end the buffer before printing it (as stated by RageZ).

  2. You increase the increment of i on each loop, but every time you type 84 characters ( bsize ) I read in and [i] I think that this should mean that you are only looking at every 84th character. Apart from this, if I were you, I would not always guarantee to return the number of bytes you expected to every fread .


    EDIT: The block you are reading is fine. I was confused by Typhief for a minute. Every time you increase with i 1, it increases the indicator with the 84 * sizeof (CHR) , however, you do not guarantee this Can give it the number of bytes that you think it did. If this decreases, you will be left in the buffer with the litter: It says that it reads 60 characters, which leaves 24 junk characters before the insertion point for the next reading.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -