From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 69197 invoked by alias); 21 Sep 2015 13:25:58 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 69160 invoked by uid 48); 21 Sep 2015 13:25:54 -0000 From: "beyzman at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/67669] New: Wrong works fwrite or fread or both Date: Mon, 21 Sep 2015 13:25:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: major X-Bugzilla-Who: beyzman at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-09/txt/msg01712.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67669 Bug ID: 67669 Summary: Wrong works fwrite or fread or both Product: gcc Version: unknown Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: beyzman at gmail dot com Target Milestone: --- Created attachment 36365 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36365&action=edit Example of file to be processed The program below is intended to replace every symbol with code \x00 by symbol \x20 in some file. The program was compiled by g++ for Windows from MinGW packet. In the example attached there is symbols with code \x00 after every symbols 'j'. It is intended that after program is executed, there will be symbols with code \x20 after every symbol 'j'. But it is not so. If I decomment 2 lines commented, the programm works as intended. //#include #include #pragma hdrstop #define min(a, b) (((a) < (b)) ? (a) : (b)) int main(int argc, char *argv[]) { FILE *f; int pos; const int bufSize = 10; int length; int piece; int i; int ret; int chSw = 0; const char *fileName = argv[1]; char *buf; if (argc < 1) printf("USAGE: 02space.exe \nReplaces every symbol \\0x00 by space\n"); f = fopen(fileName,"rb+"); if (!f) { printf("Can not open file %s",argv[1]); return -1; } fseek(f,0,SEEK_END); length = ftell(f); rewind (f); ret = 0; buf = new char[bufSize]; for(piece = (int)(min(bufSize,length)); length > 0; length -= piece,piece = (int)(min(bufSize,length))) { ++ret; pos = ftell(f) ; fread(buf,piece,1,f); chSw = 0; for (i = 0; i < piece; ++i) { if (buf[i] == '\x00') { buf[i] = ' '; chSw = 1; } } if (chSw) { fseek(f,pos,SEEK_SET); fwrite(buf,piece,1,f); // fseek(f,pos,SEEK_SET); // fread(buf, piece, 1,f); } } delete [] buf; fclose(f); return 0; }