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.
// 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;
}
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);
}
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.