Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

output to a file

Posted on 2004-10-28
6
Medium Priority
?
245 Views
Last Modified: 2010-04-15
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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:komlaaa
ID: 12440736

====================== 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
 
LVL 5

Expert Comment

by:van_dy
ID: 12441849
make sure you arent printing a stray newline anywhere
in the main() before this part of code you have posted.
0
 
LVL 5

Accepted Solution

by:
van_dy earned 500 total points
ID: 12449822
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:komlaaa
ID: 12450728
That was a wonderful catch. :)
Thanks
0
 

Author Comment

by:komlaaa
ID: 12450736
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12450767
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question