0

I really do apologize but I didn't know what part of the forum to post this in, and tutorials didnt have a post new thread link. But I noticed most beginners are still trying to understand how file processing works, streaming I/O to a file, read/write etc. So I wrote a sample program for their benefit if they want to see how it works. So I truly hope this helps and again I truly am sorry if this is in the wrong area.

// Stream Write.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h> // uses _getch() to pause at end of program/ program will not exit
#include <fstream> //file ifstream ofstream processing
#include <cstdlib>

using namespace std;


	//Decs
	char fName[32];
	char lName[32];
	char mName[32];
	char catenate[64];


int _tmain(int argc, _TCHAR* argv[])
{
	//Simple Input Output
	cout << "    First Name: "; cin >> fName;
	cout << "     Last Name: "; cin >> lName;
	cout << "   Middle Name: "; cin >> mName;

	strcpy_s ( catenate, fName );
	strcat_s ( catenate, " "   );
	strcat_s ( catenate, lName );
	strcat_s ( catenate, ", "  );
	strcat_s ( catenate, mName );

	cout << "\n\nYour Full Name: " << catenate << endl;
	cout << endl << endl;

	//-------------------------------------------------------------------------------------//

	//Simple File Stream -> Writing to the file
	//Write 1st: First Name
	//Write 2nd: Last Name
	//Write 3rd: Middle Name
	//Write 4th: Whole Name

	
	char flName[32];
	cout << " Enter a file name: "; cin >> flName;
	strcat_s (flName, ".txt" );  //Catenate '.txt' to flName
	
	ofstream ofile( flName ); //Stream out to file
	if ( ofile.fail())
	{
		cout << "Failed Opening File!";
		cerr;
	}

	//Spit out Users input into the file!

	ofile << " First Name: " << fName    << "\n";
	ofile << "  Last Name: " << lName    << "\n";
	ofile << "Middle Name: " << mName    << "\n";
	ofile << "  Full Name: " << catenate << "\n";

	//Close the output stream
	ofile.close();

	cout << endl << endl;
	cout << "File Write Process Completed!" << endl;
	cout << endl;

	//-------------------------------------------------------------------------------------//

	//Simple File Stream -> Reading from the file
	//Read 1st: First Name
	//Read 2nd: Last Name
	//Read 3rd: Middle Name
	//Read 4th: Whole Name
	
	ifstream ifile( flName ); //Stream from file to screen/display
	if ( ifile.fail())
	{
		cout << "Failed Opening File!";
		cerr;
	}

	//Pull out Users input from the file onto the screen/display!

	string dispNewLine;
	while (! ifile.eof())
	{
		getline( ifile, dispNewLine );
		cout << dispNewLine << endl;
	}		

	//Close the input stream
	ifile.close();

	cout << endl;
	cout << "File Read Process Completed!" << endl;

        //-------------------------------------------------------------------------------------//

	cout << "(O) - Open File or Exit - (E)" << endl;
	cout << "Option: "; char choice; cin >> choice;

	switch (choice)
	{
		case 'O':
					system(flName);
					break;
		case 'E':
					return 0;
		case 'o':
					system(flName);
					break;
		case 'e':
					return 0;
		default:
					cout << "Incorrect Key, try again" << endl;

	}

	cout << endl;

	_getch(); //From <conio.h>
	return 0;
}

Edited by p@rse: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by p@rse
0

Why are you using c functions in c++ code? Also you are mixing strings and char arrays. Is there a reason for that? Lastly you should not use eof() for you while loop. Just use your getline staement for your while condition.

0

Im just going by how the book taught me, I have been using Oriely c++. Most of it was from school too, I only use that because that is how I was taught, the only way I could better understand the syntax of how things work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.