Solved

output to a file

Posted on 2004-10-28
241 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
Question by:komlaaa
    6 Comments
     

    Author Comment

    by:komlaaa

    ====================== 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
    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:
    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
     

    Author Comment

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

    Author Comment

    by:komlaaa
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now