hi there, if i want to prompt user to locate the file and open, how would i do it?
for example:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
if i want to prompt user to open "example.txt" not open within the code, how would i code it so that when this programe is run, the 1st thing will ask for user to select file location and open, then continue the process such as writing "Writing this to a file". thanks
10 1983
If u want to do this with browser window then you shuld try to use VC++ MFC/COM
Raghu
nope, i dont want to do it with browser, just want to construct a simple .cpp then run. any idea?
xiaolim, have you learned about std::string and std::cin (and input in general). What you would do is ask the user (through std::cout), take in the response to a string with std::cin, and then use the string when specifying which file to open.
Or you could do it by supplying the filename as a command-line argument: -
int main(int argc, char *argv[])
-
{
-
if (argc == 2)
-
{ // One command line argument was supplied: argv[1] is
-
// a string containing the first command line argument
-
std::cout << "Output will be written to: " << argv[1] << std::endl;
-
}
-
else
-
{ // error: no arguments/too many arguments
-
std::cerr << "Usage: " << argv[0] << " filename." << std::endl;
-
return EXIT_FAILURE;
-
}
-
-
// etc...
-
}
-
hmmm. i did learned about it, but i am still very lost, but thanks, i will read up more on it.. i am sorry, very new to this.
hi arnaudk, i dont really get it, i did learn but i am very new to it. may i have more example ? such as a working simple example will do a great help, thanks. ^^ sorry for the troble
You can almost compile the code I gave you, just #include <iostream> and provide a return value for main(). Use myfile.open (argv[1]); to open the file.
As for the arguments of main(), argv is an array of (null-terminated) strings containing the command-line arguments you supplied when you execute the program, and argc is an integer which tells you the size of that array. Even if you don't supply any comand-line arguments, the size will still be 1 because the first element, argv[0], is the always name of your program. The best way to understand all this is to just compile a simple program and try it out, give it a shot and repost if you have trouble
Check google for more info and many examples.
hi, i've tried it. it seems like i still can't understand. could your explain a bit more detail?
oh.i got it already, have another question to ask -
#include <iostream>
-
#include <cstring>
-
#include <fstream>
-
#include <conio.h>
-
-
using namespace std;
-
-
-
int main(void)
-
{
-
int total=0;
-
int upper=0;
-
int lower=0;
-
int digits=0;
-
int blanks=0;
-
int eosp=0;
-
int others=0;
-
-
char buf[80];
-
ifstream fin;
-
string fname;
-
-
cout << "Welcome\n" << endl;
-
cout << "This programe will analyze the file content &" << endl;
-
cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
-
system("pause");
-
system("cls");
-
-
do
-
{
-
cout << "Enter input data file name:\n";
-
cin >> fname; // Get the file name.
-
fin.open(fname.c_str()); // Convert to C-string and open it.
-
if (!fin)
-
{ // Will fail if didn't exist.
-
cout << "Unable to open " << filename << endl;
-
cin.get();
-
system("cls");
-
}
-
} while(!fin);
-
-
-
cout << "Total number of characters: " << total << endl;
-
cout << "Number of uppercase characters: " << upper << endl;
-
cout << "Number of lowercase characters: " << lower <<endl;
-
cout << "Number of decimal digits: " << digits << endl;
-
cout << "Number of blanks: " << blanks << endl;
-
cout << "Number of end-of-sentence punctuation marks: "<< eosp << endl;
-
cout << "Others: " << others << endl;
-
cout << "" << endl;
-
-
-
system("pause");
-
-
return 0;
-
}
-
-
inside the my code, i wanted it to detect the uppercase, lowercase,digits,blanks, punctuations and others such as spaces. I've declare all the variables and assign them to initial 0.so after the file is open, the program should be able to read all the lines in the file and detects and then display them out as accordingly. any guide or help? an example will be more then enough. thank you very much
It sounds like you'll want to read the file character by character. For this, check out istream::read. To see if a character is upper/lower case, number, etc have a look at cctype.
As for accepting command line arguments, did you really try to understand it yourself with google? Compile this simple program as example.exe: -
#include <iostream>
-
-
int main(int argc, char* argv[])
-
{
-
for(int i = 0; i < argc; ++i)
-
{
-
std::cout << "Command-line argument " << i << " is: " << argv[i] << std::endl;
-
}
-
return 0;
-
}
-
Then run it: -
C:\>example.exe apple pear banana
-
Command-line argument 0 is: example.exe
-
Command-line argument 1 is: apple
-
Command-line argument 2 is: pear
-
Command-line argument 3 is: banana
-
As you can see, the arguments you supply to example.exe are stored in the array argv.
yes, is alright already, thanks for the help from you guys, i figured it out, thanks alot. ^^^
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dino |
last post by:
dear all,
i've created an application for a customer where the customer can upload
..csv-files into a specified ftp-directory. on the server, a php-script,
triggered by a cronjob, reads all the...
|
by: Chuck Amadi |
last post by:
Sorry to bovver you again (again) here's script.
I still can't see why the get_payload() doesn't produce
the plain text message body of an emails in the testwwws users mailbox.
As you can see I...
|
by: Ling Lee |
last post by:
Hi all.
I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.
2) I made the first part like this:
...
|
by: Abhas |
last post by:
> > Hi, this is Abhas,
> > I had made a video library program in C++, but was facing a problem.
> > After entering 12 movies, i cannot enter any more movies.
> > Something gibberish comes instead....
|
by: raydelex |
last post by:
I am new to securing a database with logins. My questions is: I want
only one database to use a new Workgroup file that I have created, not
all the Access databases that I bring up under my...
|
by: wetherbean |
last post by:
Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....before a playlist is created I need to check to see if...
|
by: George |
last post by:
Hi,
I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it...
|
by: prasadjoshi124 |
last post by:
Hi All,
I am writing a small tool which is supposed to fill the filesystem to a
specified percent.
For, that I need to read how much the file system is full in percent,
like the output given...
|
by: JimmyKoolPantz |
last post by:
IDE: Visual Studio 2005
Language: VB.NET
Fox Pro Driver Version: 9.0.0.3504
Problem: I currently have a problem altering a DBF file. I do not get
any syntax errors when running the program. ...
|
by: KevinADC |
last post by:
Note: You may skip to the end of the article if all you want is the perl code.
Introduction
Many websites have a form or a link you can use to download a file. You click a form button or click...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |