Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

type any file contents using simple C++ program

Posted on 2000-03-20
1
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
tomkeane earned 300 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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