Solved

Cannot send console output to file

Posted on 2002-06-27
13
235 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 236
C++ Language error 28 245
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 87
C++ mouse_event mouse look 7 96
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

856 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