Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

output to a file

I have a problem with a program.  When i run it on my screen it works fine but when i send all the output to a file it doesn't format the output properly

gotoxy(5,5);
cout << "hello";

You would see on your screen:





     hello

But if i change it so the output goes to an output file that i have opened.

gotoxy(5,5);
outputfile << "hello";

If you looked at the outputfile it would look like this.
hello

How can i fix this?  I seems like gotoxy is only for a cursor location on the screen and the outputfile just ignores it.
Any suggestions....

Thanks
0
onestar
Asked:
onestar
  • 9
  • 8
1 Solution
 
trestanCommented:
If you want to insert blank spaces in your file, you have to output the blank spaces.
Like this:
Outputfile<<"    ";//Put the length you like.
0
 
onestarAuthor Commented:
Is there any other way, I know how to do what your saying but it would take a long time for me to do that and i don't even know if i could do that for my program.  Is there anyway it could look at the gotoxy position and create something else the outputfile could read.  Because i have columns of information an that loop to find new y postions.

example

  1        hi                 this is a test                              10.00   20.00
.
.

It would take a long time to do this.
0
 
trestanCommented:
There are several ways to do.
If you want to reuse the code, I can compile a class to realize format output to a file. If you just want to realize it in a special case, you can use my method of displaying strings on screen. It will be exactly the format in the file.
Tell me what you want please.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
onestarAuthor Commented:
Is there any other way, I know how to do what your saying but it would take a long time for me to do that and i don't even know if i could do that for my program.  Is there anyway it could look at the gotoxy position and create something else the outputfile could read.  Because i have columns of information an that loop to find new y postions.

example

  1        hi                 this is a test                              10.00   20.00
.
.

It would take a long time to do this.
0
 
onestarAuthor Commented:
sorry about that double submit
0
 
trestanCommented:
For example,
If you have M=4 strings: char Strings[m][80]
and you have M=4 position for them: int Position[4]
Then:
int Totallength=0;
for (int i=0; i<4; i++)
{
if (Totallength<Position[i])
{
outputfile <<' ';
}
outputfile <<Strings[i];
Totallength+=strlen(Strings[i]);
}
}
outputfile <<"\n"
0
 
onestarAuthor Commented:
//small scale example file

#include <iostream.h>
#include <conio.h>
#include <fstream.h>


#define out_file "c:\\test\\junk.dat"

int main()
{

      ofstream outs;
      outs.open (out_file);

      clrscr();
      gotoxy(10,5);
      outs << "hi there";
      outs.close();
                return 0;
}

Here is a simple program.
Now if your were in dos and typed
type junk.dat
you would get
hi there

I need




                hi there

So i need something to look at the gotoxy and move the cursor there in the output file and type hi there.  One problem with using spaces is that if there is text before that it will be blanked out.

I hope this is some help to want i need.
0
 
trestanCommented:
Hi,
What you mean "blaked out" is that the new text overlapped the old text in the file, right?
I revised your code a little bit, if you do not mind.
 
//small scale example file

#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>



int main()
{
char out_file[]= "c:\\users\\trestan\\junk.dat" ;
char Strings[4][80]={"hi","there","1000","end"};
int Position[4] = {5,20,50,60};

fstream outputfile = fstream (out_file, ios::app);
//outs.open (out_file);

//clrscr();
//gotoxy(10,5);
int Totallength=0;
for (int i=0; i<4; i++)
{
while (Totallength<Position[i])
{
outputfile <<' '<<flush;
Totallength++;
}
outputfile <<Strings[i];
Totallength+=strlen(Strings[i]);
}
outputfile <<"\n";
//outs << "hi there";
//outs.close();
return 0;
}
 
Sorry for make mistake in using if statement. I compiled above code. No error this time.

0
 
onestarAuthor Commented:
Your program works but i still think it is going to take so long for me to put that in my program.  If i new this from the beginning it would of been so much easier but to add this into all my code is going to be tuff.
So there is no other command that works the same as gotoxy for an output file.  Why doesn't gotoxy work on an output files?
0
 
onestarAuthor Commented:
Is there away to output a number of spaces with just type a number instead
outfile << "           " << string;
example
outfile << " "[10 space] << string;


0
 
trestanCommented:
It is just for screen location.
If you feel too much things have to be revised to use my code, would you like me to make a class for you? You just need to know the parameters to be transfered to the class. Is it sounds good?

0
 
trestanCommented:
The << operator is needed to be overload to realize it.
0
 
trestanCommented:
Ok, Use this function to do just you want:

char* Blank(int Num)
{
      char *BlankString = new char[Num];
      for (int i=0; i < Num; i++)
            BlankString[i]=' ';
      return BlankString;
}

Put this function outside of your main(). Then declare:
char* BlankSpace = Blank(10);
before you use BlankSpace in your cout position you want.

0
 
trestanCommented:
It is really tricky to make it out in such a way. But I will be glad if you are satisfied.
0
 
onestarAuthor Commented:
I am going to show you my code.  This is about 1/4 of the code the rest i think i can fix for the output but this stuff is tricky.  Sorry about the lenght.  If you run this you will see what i am trying to do.  The way it outputs on your screen is what i need in a file.  I think it might get hard because of the width and formatting stuff.  It seems kind of weird that most programs output files why is it so difficult to format the output of a file.  I guess it is because i don't really know what i am doing.  If it is too much work to get this to run how i want it i understand.  If you think i should increase the points just let me know.  I am leaving for the day so  I won't be trying this until the morning.

Thanks a lot.

This is part of the program with no outputfile.  The rest of my program will open the file and stuff.  See i just thought i could change the cout's to outputfile but i guess not.


#include <iostream.h>                    
#include <conio.h>            
#include <stdlib.h>          
#include <math.h>
#include <string.h>
#include <stdio.h>

//global variables
const float tax_rate = .07;     // used to calculate tax rate.


//Functions used...



// Used to determine Money owed
void po_compute
(
char test_num[20],
char test_num2[20],
float& x,              //input:  qty_req
float& y,               //input: unit_cost
float& z               //output: extended_cost
);




void main()
{
      // Local data ...
      int item_number_c;              //check for loop to make sure
      float qty_req;                  //input: QTY REQ
      float unit_cost;                //input: UNIT COST
      float extended_cost;            //output: qty_req * unit_cost
      float tax_owed;                 //output: tax owed
      float sub_total;                //output: extended_cost++
      float grand_total;              //output: tax_owed + sub_total
      int y_pos;                      //output: used to postion new line
      char item_number[10];           //input: ITEM NO.
      char test_num2[20];             //input: char qty_req
      char test_num[20];              //input: char unit_cost
      char quit[10];                  //input: check for exit
      char string[80];                //input: DESCRIPTION
      int boundary;                   //limit: width of DESCRIPTION per line
      int line_no;                    //counter: for # of lines in DESCRIPTION


      boundary = 30;                  //set DESCRIPTION width
      y_pos = 6;                    //presets first y pos
      qty_req = 1;                  //presets  qty_req > 0
      sub_total = 0;                //presets total tax owed

      cout.setf (ios::showpoint);//turn on the showpoint flag
      cout.setf (ios::fixed);    //show all float values in fixed notation
      cout.precision (2);        //display float values accurate to 2 places

      clrscr ();            //clear screen
      line_no = 1;
      while (qty_req > 0)
      {

      y_pos = y_pos + line_no;
            if (y_pos > 0)
            {
            gotoxy (1,20);
            cout << "                                                  " << endl;

            item_number_c = 0;
                  while (item_number_c == 0)
                  {
                  gotoxy (1,20);
                  cout << "Enter Item number: ";
                  cin.getline (item_number,10);
                        if (item_number[0] == '\x0')
                        {
                        item_number_c = 0;
                        }
                        else
                        {
                        item_number_c = 1;
                        }
                  gotoxy (9,y_pos);
                  cout << item_number;
                  }
            gotoxy (1,20);
            cout << "                                      ";
            gotoxy (1,20);
            cout << "Enter Description: ";
            gets(string);
            char *s0 = string;
            char *s1;
            gotoxy (1,21);
            cout << "                                                    ";
            gotoxy (1,y_pos);
            line_no = 1;
                  while( strlen(s0) > boundary && (s1 = strchr(s0,' ')) ){
                  char *s2;
                  while( (s2 = strchr(s1+1,' ')) && s2-s0 <= boundary ){ s1=s2; }
                  *s1 = '\0';
                  cout << "                        " << s0 << endl;
                  *(s0 = s1) = ' ';
                  s0++;
                  line_no++;
                  }
                  cout << "                        " << s0 << endl;

            gotoxy (9,y_pos);
            cout << item_number;
            }

      po_compute(test_num, test_num2, qty_req, unit_cost, extended_cost);

      gotoxy (1,y_pos);
      cout.width (5);
      cout.unsetf (ios::showpoint);//turn off the showpoint flag
      cout << qty_req;

      gotoxy (55,y_pos);
      cout.width (10);
      cout.setf (ios::showpoint);//turn on the showpoint flag
      cout << unit_cost;

      gotoxy (68,y_pos);
      cout.width (12);
      cout << extended_cost;

      sub_total += extended_cost;      //used to compute total tax due.
      tax_owed = sub_total * tax_rate;
      grand_total = sub_total + tax_owed;

      gotoxy (55,17);
      cout << "Subtotal    = ";
      gotoxy (68,17);
      cout.width (12);
      cout << sub_total;

      gotoxy (55,18);
      cout << "Gst         = ";
      gotoxy (68,18);
      cout.width (12);
      cout << tax_owed;

      gotoxy (55,19);
      cout << "Grand Total = ";
      gotoxy (68,19);
      cout.width (12);
      cout << grand_total;


      gotoxy (1,20);
      cout << "                                                  " << endl;
      gotoxy (1,20);
      cout << "Do you wish to quit y for yes or n for no: ";
      cin >> quit;

            if (quit[0] == 'y' || quit[0] == 'Y')
            {
            qty_req = 0;
            }
      }
      gotoxy (1,22);
      cout << "Thank you for using PURCHASE ORDER PROGRAM V1.0";

}


void  po_compute
(
char test_num[20],
char test_num2[20],
float& x,
float& y,
float& z
)

{
gotoxy (1,20);
cout << "                                               ";
gotoxy (1,20);
cout << "Enter qty:";
cin >> test_num;
cout << endl << "Enter unit cost: " ;
      cin >> test_num2;
x = atof(test_num);
y = atof(test_num2);
z = x * y ;
}
0
 
nietodCommented:
>> So there is no other command that works the same as gotoxy for an output file.  

No, but you could make one.

What you can do is create an internal 80X25 buffer that holds all the characters currently displayed.  When you display things you would use procedures you write that both alter this buffer and output to the screen.  When you need to output to a file, you would copy this buffer to the file.  I've actually used this solution long ago (for different reasons) and it is quite powerful, but it will take some time to implement.

Another possible solution is that when you want to output to a file, read the character information from the screen buffer.  
0
 
trestanCommented:
To onestar,
I have seen your program... honestly speaking, the structure is not very well. It will be much better if you use classes...However, you are interested in file output now. The file is not like a screen which always has a physical coordinate standing there. Especially when there is already an existing file and you want to append more data into it. Nietod's buffer is a solution, other methods also available. I prefer that let's finish this question now, you can find my email address at my homepage:
http://geocities.com/SiliconValley/Network/9353/
You can tell me the requirement for your program. I would like to make a special program for you. Waiting for your answer.
0
 
onestarAuthor Commented:
Adjusted points to 150
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now