Solved

Cannot send console output to file

Posted on 2002-06-27
13
232 Views
Last Modified: 2012-06-27
Hi,
I am trying to send my console output from a program (test.exe) to a file but it is not working. The syntax I am using is:
test > file.txt

The command does stop all output appearing on my screen but it doesnt appear in the file tho, it just vanishes.
The file is created but is zero bytes.

any ideas?
0
Comment
Question by:jacy_m
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7115568
What is your OS?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7115571
Is your test.exe file sending data to standard out or standard error?
0
 

Author Comment

by:jacy_m
ID: 7115577
I am testing on NT4, but it will also have to run on Win2K

In the program I have tried
printf("blah");
cout << "blah";

but neither worked... I am not that familar with c++ programming, so I dont know what you mean by standard error...

0
 

Author Comment

by:jacy_m
ID: 7115579
actually its NT4 Server and will have to run on Win2K Server as well
0
 

Author Comment

by:jacy_m
ID: 7115584
here is my code. I figured out that if I comment out the while loop it prints blah to the file, but if I leave the while loop in it doesnt... is there some problem with my other file stream?

#include <fstream.h>

ifstream servers;

int main()
{
  char* pszServer = new char[50];
  char code;
  cout << "blah";

  servers.open("serverlist.txt");
  if (servers.fail())
  {
     cout<<"Input file opening failed. \n";
     exit(1);
  }
     
  //read server list
  int index = 0;
  while (!servers.eof())
  {
    do {
      servers.get(code);
      if (code != '\n') {
        pszServer[index] = code;
        index++;
      }
    } while (code != '\n');
    pszServer[index] = '\0';              
  }

  servers.close();
  results.close();
     
  return 0;
     
}
0
 
LVL 2

Expert Comment

by:Ber
ID: 7116079
Just a few things...!!!

What is the structure of the text file...???
(ie I assume its something like this...
abcde

or this

     a
     b
     c
     d
     e
)

Where is results declared...???

Once you read in the characters you do nothing with them...!!!

Because you do not increment the index on the last iteration you are probably overwriting the last element in the array with a null character...!!!

If you add a simple loop to print out the contents of the array to the end of your code it may help...!!!

have a look at these and see if it helps...

Cheers...
Ber...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:VEngineer
ID: 7116620
what does the input file servers.txt look like?  I can see a handful of potential problems with your code fragment.  Give us an idea of what your input file consists of and what you need to do to it and we can help you from there.

I think you can simplify a LOT of this code.  There's no real reason to read strings in one character at a time like this.  
0
 

Author Comment

by:jacy_m
ID: 7120759
sorry, i took out the bit where i process the string for simplicity

my text file looks like this:
one
two
three

i changed the program to read the file one line at a time (i didnt know i could do this - im just learning c++) and the problem with no output to a file went away... i still dont know what it was, but some problem in the while loop.
Here is the code that fixed it:

#include <fstream.h>

ifstream servers;

int main()
{
   char* pszServer = new char[50];

   servers.open("serverlist.txt");
   if (servers.fail())
   {
      cout<<"Input file opening failed. \n";
      exit(1);
   }
     
   //read server list
   while (!servers.eof())
   {
      servers.getline(pszServer,50);
      outputServer(pszServer);
   }

   servers.close();    
   return 0;
     
}
0
 
LVL 2

Expert Comment

by:Ber
ID: 7120819
Glad you got it working...

Out of curiosity...!!!
Have you tried tracing through the original code to see if you can spot the error.. I would guess the error is in the code writing out to the file rather than the code reading in the text...???

Cheers...
Ber...
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 7122343
Yup, I agree with Ber, it looks pretty good.  I'd only make a few changes (just minor):

 - use a standard string instead of a dynamically allocated character array.  This will free you from the 50 character limit.  In addition, this will eliminate your memory leak in your code.

 - make the ifstream a local variable if possible.  I see no need for it to be a global.

 - I do my file reading a little differently, you are welcome to take a look at my code sample below:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
        const string filename = "serverlist.txt";
        ifstream fin(filename.c_str());

        if (!fin) {
                cout << "Input file opening failed." << endl;
                return 1;
        }
   
        string pszServer;

        while (fin >> pszServer) {
                outputServer(pszServer);
        }

        return 0;
}
0
 

Author Comment

by:jacy_m
ID: 7123383
Ber,
Thats what i thought too.. the results were ending up in the file i was reading in but they arent there.. i just dont know!


VEngineer,
What does the line "using namespace std;" do?

In the method i call.. i use this line with 2 char arrays
strcat(pszServerDN, pszServer);
Can I still do this if I make them both strings?

thanks
0
 
LVL 2

Accepted Solution

by:
VEngineer earned 50 total points
ID: 7125336
For a really brief overview of std:: and what happened to the .h headers:
http://www.comeaucomputing.com/techtalk/#whystd

Since you are new to C++, you don't have to worry about the specific details for a while, but most any recently published (since 2000) C++ book will give you a detailed explaination on the headers and namespace.

As for your question about strcat, concatenating standard strings are even easier now.

Example:
string a = "hello";
string b = "world";
string c = a + b;   // c == "helloworld"
a += "friend";      // a == "hellofriend"

So in your specific case of:
strcat(pszServerDN, pszServer);
if you convert it to standard strings, you can do the following to get the equivalent result:

string pszServerDN;
string pszServer;

// assume values get set somewhere, then

pszServerDN = pszServerDN + pszServer;

or

pszServerDN += pszServer;

0
 

Author Comment

by:jacy_m
ID: 7187113
thanks for your explanations and time
will give you the points in order to close this question
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

24 Experts available now in Live!

Get 1:1 Help Now