• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

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>
0
komlaaa
Asked:
komlaaa
  • 3
  • 3
1 Solution
 
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
 
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
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now