Output in csv format

I have input my data through command line parameters.I am trying to get my output in .csv format. The output instead of being in the output.csv shows up in the screen. I donot know what is wrong. Please help!

If you guys could give me and example of a program that can output into a .csv file using cout. That would be a great help too.

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>


#include "arrayUtils.h"

using namespace std;

// Strips all punctuation (except hyphens) from a word and
// changes any upper-case characters to lower-case. Note that
// if a word has nothing except punctuation, this may reduce
// it to the empty string.
string reduceWords (string word)
{
string result;
for (int i = 0; i < word.size(); ++i)
{
char c = word[i];
if (c == '-')
result += c;
else if (c >= 'a' && c <= 'z')
result += c;
else if (c >= 'A' && c <= 'Z')
result += (c - 'A' + 'a'); // converts to lower-case
}
return result;
}

// Read words from the provided input stream, reducing each word and counting
// how many times each word appears. Write the resulting words and counts (in
// alphabetic order by word) in CSV format to the output stream.
// - Assume that the input contains a maximum of MaxWords distinct words
// (after reduction). If more distinct words than this are actually
// encountered, write nothing to the output stream but print an error
// message on the standard error stream.
void histogram(const int MaxWords, istream& input, ostream& output)
{
//* Declare an array of strings to hold all words
//* and an array of int to hold the number of times each word
//* encountered.
std ::string holdwords[MaxWords] ;
int wordcount[MaxWords] ;
int count =0;
// Read the words from the input and count them

string word;
while (input >> word)
{
word=reduceWords(word);
//cout <<word<<endl;
int locationsearch = seqOrderedSearch(holdwords, count,word);

if (locationsearch == -1)
{
if (count==MaxWords)
{
cerr<<"Input file contains more than " <<MaxWords<<" words."<<endl;
exit(1);
}
else
{
// adding new words in the array
int locationadd= addInOrder(holdwords,count,word);
count=count-1;
addElement(wordcount,count,locationadd, 1);
wordcount[locationadd]=1;
// cout<<"wordcount "<<wordcount[locationadd]<<endl;
}
}
else
{// its going int he same place add element should work.
//cout<<"ïam crying"<<endl;
wordcount[locationsearch]+=1;
//cout <<"adding wordcount"<<wordcount[locationsearch]<<endl;
}

}
for(int i=0;i<count;i++)
cout<< '"' << holdwords[i] << '"'<< ','<<wordcount[i] << endl;

cout <<count<<endl;
}


int main (int argc, char** argv)
{
if (argc != 2 && argc != 4)
{
cerr << "Usage: " << argv[0] << " MaxWords [inFileName outFileName]" << endl;
return -1;
}

int MaxWords = atoi(argv[1]);

if (argc == 2)
{
// No file names in command line - use standard in and standard out
histogram (MaxWords, cin, cout);
}
else
{
// Take input and output file names from the command line
ifstream in (argv[2]);
ofstream out (argv[3]);
histogram (MaxWords, in, out);
}

return 0;
}



This is my program pretty much
Last edited on
In your histogram() function you are writing to cout rather than output:
1
2
3
cout << '"' << holdwords[i] << '"' << ',' << wordcount[i] << endl; // cout?

output << '"' << holdwords[i] << '"' << ',' << wordcount[i] << endl; // did you mean this? 
The program is designed to cout into the command window when there arent any file names in the command line. It should also be able to read from file and write into a file whenever the parameters are set as such.
This has be confused.
I understand that, but your function histogram() always writes to cout (the command window) regardless.
Thanks it worked with ouput just like you suggested
Topic archived. No new replies allowed.