1

I've written a bare bones C program which reads a file and produces a table with each word and their frequency.

The program works and I have been able to get the displayed output in the terminal running on Linux, however, I'm not sure on how I would get the generated display to produce a .csv file containing the word frequency output (as it is in the terminal).

Here are code snippets of each part of my program, so you can better understand the structure of it.

int main
{
   table (int *freqCount);
   processLine (int * freqCount, char *buffer);

   ...
   printTable (results);
}

void printTable(int *results)
{
   double tableAVG (int *results);
   ... table print layout

   for (i = 1; i < MAX_WORD_LEN; ++i)
{
    if (results[i] > 0)
        printf(" %2i%11i\n", i, results[i]);
}
}

void processLine (int *results,  char *buffer)
{
    char *token;
char *delimiter = " ,.;:'\"&!? -_\n\t";

... buffer rule
...     token rule 

while (token != NULL)
{

    results[strlen(token)]++;
    token = strtok(NULL,  delimiter);

}
}

double tableAverage (int *results)
{
int i;
int words = 0;
int sum = 0;

for (i = 1; i < MAX_WORD_LEN; ++i)
{
    ... rule
}

return (double)sum/(double)words;
}

How would I send the output of the terminal to a .csv? Any help would be appreciated, thanks.

Amir Zadeh
  • 3,481
  • 2
  • 26
  • 47
a.ymous
  • 139
  • 11

2 Answers2

2

Use redirection when you start the program:

/path/to/you/executable > data.csv
Alepac
  • 1,833
  • 13
  • 24
  • how would that be structured? Could you please be a little more specific? Is this what you are talking about: http://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c – a.ymous Apr 15 '13 at 14:29
  • What OS are you using (Windows, Linux, ...)? I'm to starting the program with console redirection. Look at those link for [windows](http://technet.microsoft.com/en-us/library/bb490982.aspx) or [linux](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html). – Alepac Apr 15 '13 at 14:53
  • Is your .csv properly formatted? is so just use yourprogram > yourcsvfile. – Amir Zadeh Apr 15 '13 at 15:14
  • open a consol and run your exebutable using '>' to redirect the output to a file – Alepac Apr 15 '13 at 15:20
1

You may use > operator to direct the output of your program to a file. You may use < operator to read the input of your program from a file. You may use | operator to link the output of a program to the input of another.

Sample: write in your terminal,

helloworld.exe > helloworld.txt
Amir Zadeh
  • 3,481
  • 2
  • 26
  • 47