• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

c++ file IO

Started by James C. Fuller, November 02, 2014, 01:58:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

Fred,
  I got a bit sidetracked while coding a ddt for Pbcc and lost interest. I thought I posted a resource version of the PBWin address demo?
I decided to freshen up bc9Basic and put together some new oop examples.
My primariy focus is 64bit using VS 2013 express along with the TDM-GCC and NUWEN Mingw ports.

I came about the codeguru post because I did not like the native Bcx / bc9Basic translation of LINE INPUT.
It uses the stdio functions.
LINE INPUT  hFile, Buffer$ is translated to

fgets(Buffer,1048576,hFile)
if(Buffer[strlen(Buffer)-1]==10)Buffer[strlen(Buffer)-1]=0;


Buffer$ has a default size of 2048
It does clearly state in the help file: be sure to dimension Buffer$ large enough to accomodate the longest line in the file that is to be input.

The reason for the magic number is there is no "standard" way to get the size of the buffer if it was allocated using new or malloc.
VC++ does give you _msize but the translator was written for use with many different c and c++ compilers.
I believe VC++ does have defines that will prevent buffer overruns on char arrays but not on allocated memeory.
While you do get speed you sacrifice buffer overrun safety.

The iostream version is so nice and clean besides I am trying to use only std:(w)strings in my coding.

I frequent cprogramming.com daily and there have been many posts on  citing use of c code in supposed c++ posts.
Some I believe were because of using stdio.h instead of <cstdio> which I discovered recently.

James








Frederick J. Harris

I see.  I suppose it would be possible to use one of the api or std lib functions to get the total size of the file being read, and allocate the read buffer to that.  That would be the worst case scenario of the entire file being one line.  In the more positive case of the file being multiple lines, one would then have to parse the whole blob oneself breaking it into multiple lines each with its own memory allocation, and moving the lines to there (that is, if the whole file was read in one big gulp).