473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

File Open

18
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
Jul 28 '08 #1
10 1983
gpraghuram
1,275 Expert 1GB
If u want to do this with browser window then you shuld try to use VC++ MFC/COM

Raghu
Jul 28 '08 #2
xiaolim
18
nope, i dont want to do it with browser, just want to construct a simple .cpp then run. any idea?
Jul 28 '08 #3
oler1s
671 Expert 512MB
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.
Jul 28 '08 #4
arnaudk
424 256MB
Or you could do it by supplying the filename as a command-line argument:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.    if (argc == 2)
  4.    {  // One command line argument was supplied: argv[1] is  
  5.       // a string containing the first command line argument
  6.       std::cout << "Output will be written to: " << argv[1] << std::endl;
  7.    }
  8.    else
  9.    { // error: no arguments/too many arguments
  10.       std::cerr << "Usage: " << argv[0] << " filename." << std::endl;
  11.       return EXIT_FAILURE;
  12.    }
  13.  
  14.    // etc...
  15. }
  16.  
Jul 28 '08 #5
xiaolim
18
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
Jul 28 '08 #6
arnaudk
424 256MB
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.
Jul 28 '08 #7
xiaolim
18
hi, i've tried it. it seems like i still can't understand. could your explain a bit more detail?
Jul 29 '08 #8
xiaolim
18
oh.i got it already, have another question to ask
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <cstring>  
  3. #include <fstream>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main(void)
  10. {
  11.     int total=0;
  12.     int upper=0;
  13.     int lower=0;
  14.     int digits=0;
  15.     int blanks=0;
  16.     int eosp=0;
  17.     int others=0;
  18.  
  19.     char buf[80];
  20.     ifstream fin;
  21.     string fname;
  22.  
  23.     cout << "Welcome\n" << endl;
  24.     cout << "This programe will analyze the file content &" << endl;
  25.     cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
  26.     system("pause");
  27.     system("cls");
  28.  
  29.     do 
  30.        {
  31.         cout << "Enter input data file name:\n";
  32.         cin >> fname;     // Get the file name.
  33.         fin.open(fname.c_str());  // Convert to C-string and open it.
  34.         if (!fin) 
  35.              {          // Will fail if didn't exist.
  36.                 cout << "Unable to open " << filename << endl;
  37.                 cin.get();
  38.                 system("cls");
  39.              } 
  40.         } while(!fin);
  41.  
  42.  
  43.     cout << "Total number of characters: " << total << endl;
  44.     cout << "Number of uppercase characters: " << upper << endl;
  45.     cout << "Number of lowercase characters: " << lower <<endl;
  46.     cout << "Number of decimal digits: " << digits << endl;
  47.     cout << "Number of blanks: " << blanks << endl;
  48.     cout << "Number of end-of-sentence punctuation marks: "<< eosp << endl;
  49.     cout << "Others: " << others << endl;
  50.     cout << "" << endl;
  51.  
  52.  
  53.        system("pause");
  54.  
  55. return 0;
  56. }
  57.  
  58.  
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
Jul 29 '08 #9
arnaudk
424 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5.   for(int i = 0; i < argc; ++i)
  6.   {
  7.     std::cout << "Command-line argument " << i << " is: " << argv[i] << std::endl;
  8.   }
  9.   return 0;
  10. }
  11.  
Then run it:
Expand|Select|Wrap|Line Numbers
  1. C:\>example.exe apple pear banana
  2. Command-line argument 0 is: example.exe
  3. Command-line argument 1 is: apple
  4. Command-line argument 2 is: pear
  5. Command-line argument 3 is: banana
  6.  
As you can see, the arguments you supply to example.exe are stored in the array argv.
Jul 29 '08 #10
xiaolim
18
yes, is alright already, thanks for the help from you guys, i figured it out, thanks alot. ^^^
Jul 31 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

18
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...
16
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...
22
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: ...
3
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....
1
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...
19
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...
13
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...
14
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...
9
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. ...
1
KevinADC
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.