Solved

type any file contents using simple C++ program

Posted on 2000-03-20
1
176 Views
Last Modified: 2012-05-04
My problem is that the program I wrote in C++ is not displaying what it should show. All I want it to do is to accept any path & filename & display its contents. My program is the following:

#include<iostream.h>
#include <fstream.h>
#include <iomanip.h>
main(char *path)
{
      ifstream textfile(path, ios::in);
      while (!textfile.eof()){
            char ch;
            textfile.get(ch);
            cout << ch;
      }
      textfile.close();
      return 0;
}
0
Comment
Question by:Samer Kharsa
1 Comment
 
LVL 1

Accepted Solution

by:
tomkeane earned 100 total points
ID: 2638080
You need to change your declaration of main as shown below.  Main  gets passed an  array of strings (found in argv).  argc contains the number of elements in the array.  The first element is the name of the program that is running, subsequent command line entries follow.

In this case, the command line to run the program  would be something like:

mylist somefile.txt

In this case argc would have the value of 2, and argv would have the values:
argv[0] = mylist
argv[1] = somefile.txt

The modified version of your program worked for me on VC6-sp3.

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

main(int argc, char **argv)
{
  ifstream textfile(argv[1], ios::in);
  while (!textfile.eof()){
    char ch;
    textfile.get(ch);
    cout << ch;
  }
  textfile.close();
  return 0;
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now