I want to write all the output to the output file and don't want to print on the console output...the file (in which the output has been written) should be opened after i run the program.

which function to use for opening a file to see output( at run time)??

Thanks & Regards.

@rajatC
your question is not clear. Do you want to redirct standered output?
You can use CreateFile API to open the file. However, keep this in mind, and make sure there is no sharing violation.

ok...let me restate my problem..
see this code snippet

int main()
{
  ofstream fout("output.txt");
  //processing.
 //output to file
 //using fout
 //no console output
 
 //showFile(output.txt);
 return 0;
}

see the second last line of my code... the output has been saved to output.txt.. no console output..
when i run the program the command window will blink...as i have not used system("pause") to retain it...

what i want to do with showFile function is that it will open the file output.txt so that user can see the file...

i hope now you got it...

do it the same way as you would with cout to print to the console fout << "Hello World\n"; . The syntax is identical to cout. Then close the file fout.close(); before calling your showFile() function so that everything gets physically written to the disk.

@ancient dragon..

you didn't get my question...i know how to output to file...but i wanted to ask is there any function which will open the file for user to see the output...
is there any showFile() type of function in libraries??

>is there any showFile() type of function in libraries??
No, you have to write it yourself.

I DONT know how to do that...can you help me in writing that...
how a file would be opened for user to show the output by the code itself??

>I DONT know how to do that...
I'm willing to bet you do, but you're just not making the connection:

#include <fstream>
#include <string>

// Open a file and display it
void showFile ( const char *filename )
{
  std::ifstream in ( filename );
  std::string line;

  while ( std::getline ( in, line ) )
    std::cout<< line <<'\n';
}

Though I'll go out on a limb and wager that you don't want console output, but rather you want to start some random text editor that will display the file for you. That's non-portable, but you might do something like this:

#include <cstdlib>
#include <string>

// Open a file and display it in the specified program
void showFile ( const std::string& program, const std::string& filename )
{
  std::system ( ( program + " " + filename ).c_str() );
}

Of course the user could always use Notepad to view the file.

>Of course the user could always use Notepad to view the file.
Oddly enough, my test call for that function was:

showFile ( "notepad.exe", "C:\\narue_is_kewl.txt" );

;)

>Of course the user could always use Notepad to view the file.
Oddly enough, my test call for that function was:

showFile ( "notepad.exe", "C:\\narue_is_kewl.txt" );

;)

thanks for the help...all that you told me is working fine....

now i have another problem...i want to open file with some other programs...like MS word,MS excel etc...

so what should be the program name (eg notepad.exe) for them....how can i find the exact name for all the text reading programs in my laptop..(wordpad,MS excel,MS access,MS word etc..)

Member Avatar for iamthwee

Reading and writing stuff from MS Office is a lot more difficult.

yeah i know that....i am trying to learn that also..
here just tell me how to open a file in excel or word through program..

Member Avatar for iamthwee

So you just want to open it.

Then I guess the easiest, although not necessarily the best way, would be to call it from system.

#include <iostream>
using namespace std;

int main()
{
   system("C:/test.xls"); //path to excel file
}

So you just want to open it.

Then I guess the easiest, although not necessarily the best way, would be to call it from system.

#include <iostream>
using namespace std;

int main()
  {
   system("C:/test.xls"); //path to excel file
}

i tried it..and it is working absolutely fine...my code is

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream fout("excel.csv");
    
    int a[20];
    for(int i=0 ; i<20 ; i++)
    {
            srand(i);
            a[i]=rand()%100;
           fout<<a[i]<<",";
           if(i==10)
           fout<<"\n";
    }
   fout.close();
    system(("excel.csv"));
    return 0;
}

i know that excel files need to be saved in CSV format...(i learned it here in other thread) now as u can see that i have just mentioned the filename in system call...while you wrote the path of the file...
but my program is still working..the file is getting opened on running the program..but how?
my program is saved in F drive and i haven't mentioned the file path..but it is in the same folder where the program is saved...may be that's why it is working without filepath..please clearify!

and my other question is that it doesn't require the name of the program in which the file has to be opened..as Naure told me to write the name of the program (see older posts) for example notepad.exe ? So i think it opens the file in the default program attached to that particular format of file or extension..is it correct?

Member Avatar for iamthwee

Yeah, that sounds about right.

Btw your program is not generating random numbers properly.

Btw your program is not generating random numbers properly.

what do you mean by not properly?
are you talking about seed i ?? well i wrote this program just to learn about writing to excel and opening it...i used rand function to save myself from typing and initializing the array, nothing else.

I should have used the time seed instead to generate random numbers..
is it that what you want to say??

suggestions:
1. move line 10 srand() up above the loop. It should only be called once within the lifetime of the program. most people call it like this: srand( time(0) ); 2. why do you need the array?

suggestions:
2. why do you need the array?

hmm...correct i dont need array....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.