• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

bc9Basic Loves C++

Started by James C. Fuller, March 08, 2016, 12:18:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 31_02. Brief examples of file I/O
'Example #2
'Before you use Example C31_02 be sure to create the test.txt file with Example C31_01
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main () As int
    Raw As fstream f
    Raw As char c

    cout << "What's inside the test.txt file" << endl
       cout << endl

       f.open("test.txt", ios::in)

    while Not f.eof()
        f.get(c)                    ' Or c = f.get()
        cout << c
    Wend
   
    f.close()
    Pause
    Function = EXIT_SUCCESS
End Function
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*


Result:
What's inside the test.txt file

This is a text output to a file.
A number: 345

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 32. Character arrays can be used like files
'Generally speaking, it is possible to do on character arrays the same operations
'as on files. This is very useful to convert data or manage memory arrays.
'Here is a program that writes inside a character array.
'Example #1
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
'******************************************************************************
' g++ whines about depreciated header file but this example needs it.
' It is NOT a default #include
'******************************************************************************
#include <strstream>
'------------------------------------------------------------------------------
Function main () As int
    Raw As char a[1024]
    Raw As ostrstream b(a, 1024)

    b.seekp(0)                            ' Start from first char.
    b << "2 + 2 = " << 2 + 2 << ends      ' ( ends, not endl )
                                         ' ends is simply the
                                         ' null character   '\0'
    cout << a << endl
   
    Raw As double v = 2
   
    a$ = "A sinus: "
   
    b.seekp(Len (a))
    b << "sin (" << v << ") = " << sin(v) << ends
   
    cout << a << endl
    Pause
End Function

Result:
2 + 2 = 4
A sinus: sin (2) = 0.909297

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 32. Character arrays can be used like files
'Example #2 A program that reads from a character string
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"

'------------------------------------------------------------------------------
'g++ whines about depreciated but this example needs it. it is NOT a default #include
#include <strstream>
'------------------------------------------------------------------------------
Function main () As int
    Raw As char a[1024]
    Raw As istrstream b(a, 1024)

    a$ = "45.656"

    Raw As double k, p

    b.seekg(0)                           ' Start from first character.
    b >> k

    k = k + 1

    cout << k << endl

    a$ = "444.23 56.89"

    b.seekg(0)
    b >> k >> p

    cout << k << ", " << p + 1 << endl
    Pause
    Function = EXIT_SUCCESS
End Function


Result:
46.656
444.23, 57.89

James C. Fuller


'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 33. An example of formatted output
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"

'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Function main () As int
    Raw As int i

    cout << "A list of numbers:" << endl
    xFor i = 1 While i <= 1024 By i *= 2
        cout.width (7)
        cout << i << endl
    xNext
   
    cout << "A table of numbers:" << endl
    xFor i = 0 While i <= 4 By i++
        cout << setw(3) << i << setw(5) << i * i * i << endl
    xNext
   
    Pause
    Function = EXIT_SUCCESS
End Function

Result:
A list of numbers:
      1
      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024
A table of numbers:
  0    0
  1    1
  2    8
  3   27
  4   64