Hi,

Could anyone please tell me how can i input data either from a file or stdin.

If the file name is not specified the data can be read from the UNIX command prompt using stdin.

I know stdin is a file pointer but how to use it please explain??

you mean something like this? You can either provide a file name on the command line or redirect a file into the program like this: myprog <main.cpp

void foo(istream &in)
{
    string line;
    while( getline(in, line) )
        cout << line << '\n';
}

int main(int argc, char* argv[])
{
    if( argc == 2)
    {
        ifstream in(argv[1]);
        if( in.is_open() )
            foo(in);
    }
    else
        foo(cin);
}

yes that is what i wanted to either provide a file name on the command line or redirect a file into the program.

But i have not used ifstream for opening the file.I have used a file pointer and opened the file using fopen.

could you please tell me how to redirect the file using FILE pointer.

My assignment says like this:

The commandline syntax for hw2 is as follows:

hw2 bst [-displayall] [file]
hw2 bst_delete [-displayall] string1 string2 ... stringN file
hw2 avl [-displayall] [file]
hw2 avl_delete [-displayall] string1 string2 ... stringN file

Square bracketed items are optional. [BC: updated 2/13/2010] If an optional file is not specified, your program should read from stdin. You must follow the UNIX convention that commandline options can come in any order. (Note: a commandline option is a commandline argument that begins with a - character in a commandline syntax specification.) Unless otherwise specified, output of your program must go to stdout and error messages must go to stderr.

This is the C++ forum, not the C forum. You cannot expect C answers in a C++ forum.

Nevertheless, AD did answer your question sufficiently. The answer works the same way, except you use a FILE* instead of an istream.

If you can use FILE*s, you can do it. Please pay attention.

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.