Solved

type any file contents using simple C++ program

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

808 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