Skip to content

Instantly share code, notes, and snippets.

@sms420
Created June 30, 2009 00:18
Show Gist options
  • Select an option

  • Save sms420/137906 to your computer and use it in GitHub Desktop.

Select an option

Save sms420/137906 to your computer and use it in GitHub Desktop.
/* input_temp_output.cpp
reads from, line by line, an input text file
writes to temp the reads from temp and writes to out
*********************************************************/
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
int main () {
string temp_string;// temporary string to hold line of data
ifstream inputFile ("in.txt");
ofstream tempFile_writeTo;
tempFile_writeTo.open("temp.txt");
if (inputFile.is_open() && tempFile_writeTo)
{
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 tempFile character, by character
for (int i = 1; i < temp_string.size(); i++)
{
// if the machine encounters a comma, add newline
// and delete the the comma. start loop again
if (strncmp (&a[i],"*",1) == 0)
{
tempFile_writeTo << endl;
continue;
}
else if (strncmp (&a[i],"\"",1) == 0)
{
continue;
}
else
{ tempFile_writeTo << a[i]; }
}//closes for loop, we've iterated over one line, woohoo
tempFile_writeTo << endl; //add endline before next entry
} // closes: while (! inputFile.eof() )
inputFile.close();
tempFile_writeTo.close();
} // closes: (inputFile.is_open() && tempFile_writeTo)
/* WE HAVE CLEANED UP ALL THE EXTRA PUNCTUATION
AND NOW ALL DATA IS ON A SEPARATE ROW. LET'S
START READING FRoM THE TEMP AND WRITING ***/
ifstream tempFile_readFrom ("temp.txt");
ofstream outputFile;
outputFile.open("out_dev.txt");
if (tempFile_readFrom.is_open() && outputFile.is_open())
{
while (! tempFile_readFrom.eof() )
{
getline (tempFile_readFrom,temp_string);
char *a=new char[temp_string.size()+1];
a[temp_string.size()]=0;
memcpy(a,temp_string.c_str(),temp_string.size());
//let's just print one to one and then hack
for (int i = 0; i < temp_string.size(); i++)
{
outputFile << a[i];
}
outputFile << endl;
} // closes: while (! tempFile_readFrom.eof() )
tempFile_readFrom.close();
outputFile.close();
} // if (tempFile_readFrom.is_open() && outputFile.is_open())
return 0;
} //** closes: main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment