Cannot send console output to file

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?
jacy_mAsked:
Who is Participating?
 
VEngineerConnect With a Mentor Commented:
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
 
AxterCommented:
What is your OS?
0
 
AxterCommented:
Is your test.exe file sending data to standard out or standard error?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jacy_mAuthor Commented:
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
 
jacy_mAuthor Commented:
actually its NT4 Server and will have to run on Win2K Server as well
0
 
jacy_mAuthor Commented:
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
 
BerCommented:
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
 
VEngineerCommented:
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
 
jacy_mAuthor Commented:
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
 
BerCommented:
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
 
VEngineerCommented:
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
 
jacy_mAuthor Commented:
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
 
jacy_mAuthor Commented:
thanks for your explanations and time
will give you the points in order to close this question
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.