Solved

type any file contents using simple C++ program

Posted on 2000-03-20
1
211 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 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

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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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