Hi!
I made a visual c++ application. My problem is how do I run the program in the console. And the program should read the data from sample.txt file

Recommended Answers

All 5 Replies

Press 'F5' to compile and run the program. The console window will open automatically.

Press 'F5' to compile and run the program. The console window will open automatically.

I think u misunderstood me. I need to open the exe file in the console and add another txt file to the program so that the program can read the data from the file.
e.g. C:\> sample < sample.inn
But I can't this exe file in the directory

Thanks

In that case:
- click start
- click run
- type 'cmd'
- go to your project directory with the 'cd' command
(for example cd c:\program files\blabla\yourproject\release )
- type in your executables name

You have lots of choices. niek_e has given the outline of one way. Others would include.

1) Identify the file to read.
This may be as simple as the file name and extension if the file is in the same directory as the exe of the program or it may be a formal path starting with the drive name followed by each directory and subdirectory until you get to the file name and extension. This information may be: entered by the user at the command prompt or entered by the user when prompted by the program or read from a given file of file names or from some other source.

2) associate the file identity with a file stream.
The file identity must be in the form of a C style string. It may be associated with the file stream at declaration of the file stream or by using the open() stream method. For example:

string fileIdentity;
cout << "enter file to read";
getline(cin, fileIdentitiy);
ifstream fin(fileIdentity.c_str());

3) Then use the stream to read the file and do something with the information:

char ch;
while(fin >> ch)
   cout << ch;

Tanks to both of you. I made it ! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.