Skip to content

Instantly share code, notes, and snippets.

@sms420
Created June 13, 2009 01:08
Show Gist options
  • Save sms420/129048 to your computer and use it in GitHub Desktop.
Save sms420/129048 to your computer and use it in GitHub Desktop.
readFrom_WriteTo_TextFile.cpp
/* readFrom_WriteTo_TextFile.cpp
reads from, line by line, a text file: INPUT.txt
writes to, line by line, a text file: OUTPUT.txt
*********************************************************/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main () {
int result;
string temp_string;// temporary string to hold line of data
ifstream inputFile ("INPUT.txt");
ofstream outputFile;
outputFile.open("OUTPUT.txt");
if (inputFile.is_open())
{
while (! inputFile.eof() )
{
// read line from INPUT.txt & copy to temp_string
getline (inputFile,temp_string);
// dynamically alocate enough memory for char array
char *a=new char[temp_string.size()+1];
// assign zero to the first value in the array
a[temp_string.size()]=0;
// copy data from temp_string into char array
memcpy(a,temp_string.c_str(),temp_string.size());
// write to outputFile character, by character
for (int i = 0; i < temp_string.size(); i++)
{
outputFile << a[i];
}
outputFile << endl;
} // closes: while (! inputFile.eof() )
inputFile.close();
outputFile.close();
} // closes: if (inputFile.is_open())
return 0;
} //** closes: main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment