Skip to content

Instantly share code, notes, and snippets.

@sms420
Created June 29, 2009 07:37
Show Gist options
  • Select an option

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

Select an option

Save sms420/137517 to your computer and use it in GitHub Desktop.
/* doSomethingWhenYouFindaCSV.cpp
reads from, line by line, a text file: INPUT.txt
adds a newline when it encounters a comma
writes to, line by line, a text file: OUTPUT.txt
*********************************************************/
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
int main () {
int result;
string temp_string;// temporary string to hold line of data
ifstream inputFile ("in.txt");
ofstream outputFile;
outputFile.open("out_test.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 = 1; i < temp_string.size(); i++)
{
// if the machine encounters a comma, add newline
if (strncmp (&a[i],",",1) == 0)
{
outputFile << endl;
continue;
}
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