• C

output to a file

when i run the code as shown below, and print the output to an output file, a blank line is always printed first, behavior i don't actually want. i can't find out what is causing this.

========================= a sample run ==========
crux:~/achille/a4_1> ./cs450words -d ../a5/komlaaa.4.wordExamples.input > f.out
crux:~/achille/a4_1> cat f.out

DISTINCT: 19
crux:~/achille/a4_1> ./cs450words -c ../a5/komlaaa.4.wordExamples.input > f.out
crux:~/achille/a4_1> cat f.out

MOST COMMON WORD: 0-a 1
crux:~/achille/a4_1> ./cs450words -t ../a5/komlaaa.4.wordExamples.input > f.out
crux:~/achille/a4_1> cat f.out

TOTAL: 19
crux:~/achille/a4_1>
komlaaaAsked:
Who is Participating?
 
van_dyCommented:
hello komla,
         please take a look into wc.cpp


void wc::read(std::istream* f)
{
        std::string word;
        int count = 0;

        (*f) >> word;


        while((*f)>>word){
                removePunc(word);

                if(passWordTest(word)){
                          if( words.find(word) == words.end()  )
                           words[word] = 0;
                         words[word]++;
                         count++; //count the number of word stored.
                }
        }
  if(words.size() == 0)
{
  std::cout<<"Not a word file\n";
  exit(0);
  std::cout<<endl;                // this is what you need to remove
  totalCount = count;
  distinctCount = words.size();
}

0
 
komlaaaAuthor Commented:

====================== PIECE OF MAIN =======================
well... the parts of my main the print those output are below and see the .h for the implementation of those function:
 if( true == checkT ){
    std::cout<<"TOTAL: "<<w.getTotalCount()<<endl;
  }

  if( true == checkD ){
    std::cout<<"DISTINCT: " <<w.getDistinctCount()<<endl;
  }

 if( true == checkC ){
   std::cout<<"MOST COMMON WORD: "<<w.getMostCommonWord()<<" "<<w.getMostCommonCount()<<endl;
 }

 if( true == checkL ){
  w.display(w.begin(), w.end());
 }


0
 
van_dyCommented:
make sure you arent printing a stray newline anywhere
in the main() before this part of code you have posted.
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
komlaaaAuthor Commented:
That was a wonderful catch. :)
Thanks
0
 
komlaaaAuthor Commented:
is it because we i am using std::cout and not simply cout tha std::cout is printing into file and not on screen?
0
 
van_dyCommented:
std::cout  identifies the 'cout' of the standard namespace. suppose
you use something like

using namspace std;

then you can use cout . however if you dont give the
abovce using directive, the compiler needs to be told what namespace
it should search for to find the cout. hence we specify std::cout.

Now directing the output to a file by a command like

crux:~/achille/a4_1> ./cs450words -t ../a5/komlaaa.4.wordExamples.input > f.out

is the responsibiloity of the shell, not of the cout operator.  when the shell executes
a command(like cs450words), it has 3 files open for it by default, STDIN, STDOUT, STDERR.
most of the times these are the terminal. However '< file' makes STDIN = file, '> file2' makes STDOUT =
files2' and '2> file3' makes  STDERR = file3.

like in above example you have directed your output to f.out, std::cout is simply writing on the
STDOUT, which in this case happens to be open at f.out
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.