Difference: CodeFiles (1 vs. 3)

Revision 32015-12-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Files

Line: 375 to 379
  %ENDCODE%
Added:
>
>

File Example reverse lines

 
Changed:
<
<
>
>
%CODE{"c++"}%
 
Added:
>
>
#include #include #include #include
 
Added:
>
>
int main( ) { using namespace std; ifstream in_stream; ofstream out_stream;
 
Added:
>
>
in_stream.open("infile.dat"); if (in_stream.fail( )) { cout << "Input file opening failed.\n"; exit(1); }

out_stream.open("outfile.dat"); if (out_stream.fail( )) { cout << "Output file opening failed.\n"; exit(1); }

vector strings; string s; getline(in_stream,s); while (in_stream.eof()) { strings.push_back(s); getline(in_stream,s); }

for (int k = strings.size()-1 ; k >= 0 ; k-- ) { out_stream << strings.at(k) << endl; }

in_stream.close( ); out_stream.close( );

return 0; } %ENDCODE%

 
META FILEATTACHMENT attachment="infile.dat" attr="" comment="" date="1447330367" name="infile.dat" path="infile.dat" size="12" user="JimSkon" version="1"
META FILEATTACHMENT attachment="numbers.dat" attr="" comment="" date="1447330391" name="numbers.dat" path="numbers.dat" size="12" user="JimSkon" version="1"

Revision 22015-11-12 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Files

Line: 34 to 34
  return 0; } %ENDCODE%
Changed:
<
<
>
>
 

File I/O with Checks on open

Line: 115 to 114
 

%ENDCODE%

Changed:
<
<
>
>
 

Inputting a File Name

Line: 174 to 173
 

%ENDCODE%

Changed:
<
<
>
>
 

Formatting Output

Line: 250 to 249
 

%ENDCODE%

Added:
>
>
 
Changed:
<
<

>
>

Checking Input

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 6.7 Checking Input //Program to demonstrate the functions new_line and get_input. #include using namespace std;

void new_line( ); //Discards all the input remaining on the current input line. //Also discards the '\n' at the end of the line. //This version works only for input from the keyboard.

void get_int(int& number); //Postcondition: The variable number has been //given a value that the user approves of.

int main( ) { int n;

get_int(n); cout << "Final value read in = " << n << endl << "End of demonstration.\n"; return 0; }

//Uses iostream: void new_line( ) { char symbol; do { cin.get(symbol); } while (symbol = '\n'); } //Uses iostream: void get_int(int& number) { char ans; do { cout << "Enter input number: "; cin >> number; cout << "You entered " << number << " Is that correct? (yes/no): "; cin >> ans; new_line( ); } while ((ans = 'Y') && (ans = 'y')); }

  %ENDCODE%
Changed:
<
<

>
>

Editing a File of Text

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 6.8 Editing a File of Text //Program to create a file called cplusad.dat that is identical to the file //cad.dat, except that all occurrences of 'C' are replaced by "C++". //Assumes that the uppercase letter 'C' does not occur in cad.dat except //as the name of the C programming language.

#include #include #include using namespace std;

void add_plus_plus(ifstream& in_stream, ofstream& out_stream); //Precondition: in_stream has been connected to an input file with open. //out_stream has been connected to an output file with open. //Postcondition: The contents of the file connected to in_stream have been //copied into the file connected to out_stream, but with each 'C' replaced //by "C++". (The files are not closed by this function.)

int main( ) { ifstream fin; ofstream fout;

cout << "Begin editing files.\n";

fin.open("cad.dat"); if (fin.fail( )) { cout << "Input file opening failed.\n"; exit(1); }

fout.open("cplusad.dat"); if (fout.fail( )) { cout << "Output file opening failed.\n"; exit(1); }

add_plus_plus(fin, fout); fin.close( ); fout.close( );

cout << "End of editing files.\n"; return 0; }

void add_plus_plus(ifstream& in_stream, ofstream& out_stream) { char next; in_stream.get(next); while (! in_stream.eof( )) { if (next == 'C') out_stream << "C++"; else out_stream << next;

in_stream.get(next); } }

  %ENDCODE%
Added:
>
>

META FILEATTACHMENT attachment="infile.dat" attr="" comment="" date="1447330367" name="infile.dat" path="infile.dat" size="12" user="JimSkon" version="1"
META FILEATTACHMENT attachment="numbers.dat" attr="" comment="" date="1447330391" name="numbers.dat" path="numbers.dat" size="12" user="JimSkon" version="1"
META FILEATTACHMENT attachment="data.txt" attr="" comment="" date="1447330413" name="data.txt" path="data.txt" size="41" user="JimSkon" version="1"
META FILEATTACHMENT attachment="rawdata.dat" attr="" comment="" date="1447330445" name="rawdata.dat" path="rawdata.dat" size="82" user="JimSkon" version="1"
META FILEATTACHMENT attachment="cad.dat" attr="" comment="" date="1447330455" name="cad.dat" path="cad.dat" size="124" user="JimSkon" version="1"

Revision 12015-11-12 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Files

Simple File Input/Output

<-- SyntaxHighlightingPlugin -->
//DISPLAY 6.1 Simple File Input/Output
//Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat.
//(A better version of this program will be given in Display 6.2.)
#include <fstream>

int main( )
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("infile.dat");
    out_stream.open("outfile.dat");

    int first, second, third;
    in_stream >> first >> second >> third;
    out_stream << "The sum of the first 3\n"
               << "numbers in infile.dat\n"
               << "is " << (first + second + third)
               << endl;

    in_stream.close( );
    out_stream.close( );

    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->

File I/O with Checks on open

<-- SyntaxHighlightingPlugin -->
//DISPLAY 6.2 File I/O with Checks on open
//Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat. 
#include <fstream>
#include <iostream>
#include <cstdlib>

int main( )
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("infile.dat");
    if (in_stream.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    out_stream.open("outfile.dat");
    if (out_stream.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

    int first, second, third;
    in_stream >> first >> second >> third;
    out_stream << "The sum of the first 3\n"
               << "numbers in infile.dat\n"
               << "is " << (first + second + third)
               << endl;

    in_stream.close( );
    out_stream.close( );

    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->

Appending to a File

<-- SyntaxHighlightingPlugin -->
//DISPLAY 6.3 Appending to a File
//Appends data to the end of the file data.txt.
#include <fstream>
#include <iostream>

int main( )
{
    using namespace std;

    cout << "Opening data.txt for appending.\n";
    ofstream fout;
    fout.open("data.txt", ios::app);
    if (fout.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    fout << "5 6 pick up sticks.\n"
         << "7 8 ain't C++ great!\n";

    fout.close( );
    cout << "End of appending to file.\n";
 
   return 0;
}


 
<-- end SyntaxHighlightingPlugin -->

Inputting a File Name

<-- SyntaxHighlightingPlugin -->
//DISPLAY 6.4 Inputting a File Name
//Reads three numbers from the file specified by the user, sums the numbers,
//and writes the sum to another file specified by the user. 
#include <fstream>
#include <iostream>
#include <cstdlib>

int main( )
{
    using namespace std;
    char in_file_name[16], out_file_name[16];
    ifstream in_stream;
    ofstream out_stream;

    cout << "I will sum three numbers taken from an input\n"
         << "file and write the sum to an output file.\n";
    cout << "Enter the input file name (maximum of 15 characters):\n";
    cin >> in_file_name;
    cout << "Enter the output file name (maximum of 15 characters):\n";
    cin >> out_file_name;
    cout << "I will read numbers from the file "
         << in_file_name << " and\n"
         << "place the sum in the file "
         << out_file_name << endl;

    in_stream.open(in_file_name);
    if (in_stream.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    out_stream.open(out_file_name);
    if (out_stream.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
    int first, second, third;
    in_stream >> first >> second >> third;
    out_stream << "The sum of the first 3\n"
               << "numbers in " << in_file_name << endl
               << "is " << (first + second + third)
               << endl;
    in_stream.close( );
    out_stream.close( );

    cout << "End of Program.\n";
    return 0;
}


 
<-- end SyntaxHighlightingPlugin -->

Formatting Output

<-- SyntaxHighlightingPlugin -->
//DISPLAY 6.6 Formatting Output 
//Illustrates output formatting instructions.
//Reads all the numbers in the file rawdata.dat and writes the numbers
//to the screen and to the file neat.dat in a neatly formatted way.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void make_neat(ifstream& messy_file, ofstream& neat_file,
             int number_after_decimalpoint, int field_width);
//Precondition: The streams messy_file and neat_file have been connected
//to files using the function open.
//Postcondition: The numbers in the file connected to messy_file have been
//written to the screen and to the file connected to the stream neat_file. 
//The numbers are written one per line, in fixed-point notation (that is, not in 
//e-notation), with number_after_decimalpoint digits after the decimal point; 
//each number is preceded by a plus or minus sign and each number is in a field 
//of width field_width. (This function does not close the file.)

int main( )
{
    ifstream fin;
    ofstream fout;

    fin.open("rawdata.dat");
    if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    fout.open("neat.dat");
    if (fout.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
 
    make_neat(fin, fout, 5, 12);

    fin.close( );
    fout.close( );

    cout << "End of program.\n";
    return 0;
}
//Uses iostream, fstream, and iomanip:
void make_neat(ifstream& messy_file, ofstream& neat_file,
              int number_after_decimalpoint, int field_width)
{
    neat_file.setf(ios::fixed);
    neat_file.setf(ios::showpoint);
    neat_file.setf(ios::showpos);
    neat_file.precision(number_after_decimalpoint);
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.setf(ios::showpos);
    cout.precision(number_after_decimalpoint);

    double next;
    while (messy_file >> next)
    {
        cout << setw(field_width) << next << endl;
        neat_file << setw(field_width) << next << endl;
    }
}


 
<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback