Skip to content

Instantly share code, notes, and snippets.

@sms420
Created June 14, 2009 20:16
Show Gist options
  • Save sms420/129799 to your computer and use it in GitHub Desktop.
Save sms420/129799 to your computer and use it in GitHub Desktop.
CSV_One_Vertical_Line.cpp
/* CSV_One_Vertical_Line.cpp
reads from, line by line, a text file: INPUT.txt
adds a newline when it encounters a comma
removes commas and quotes from a CSV file
writes to, line by line, a text file: OUTPUT.txt
*********************************************************/
#include<iostream>
#include<fstream>
#include<string.h>
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++)
{
// if the machine encounters a comma, add newline
if (strncmp (&a[i],",",1) == 0)
{ outputFile << endl; }
else if (strncmp (&a[i],"\"",1) == 0)
{ continue; }
else
{ 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