Solved

type any file contents using simple C++ program

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 91
How to split this in C++ 4 119
C++ question 3 75
Android development question 2 81
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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