Computing.Net > Forums > Programming > user specified input file in c++

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

user specified input file in c++

Reply to Message Icon

Original Message
Name: scott
Date: March 10, 2003 at 21:31:05 Pacific
Subject: user specified input file in c++
OS: xp pro
CPU/Ram: 800mhz t-bird, 256 pc133
Comment:

my c++ assignment requires me to have the user enter a string and search a file for number of occurences. this isn't too hard except that the user is supposed to enter the name of the file to input from and output to in a string. using ifstream, how can I open a file selected by a string. eg:

ifstream input;
input.open("user inputted string");

can I just create a string like:
char *my_string and use it like this? -->

ifstream input;
input.open(my_string);

can this be done? somebody please let me know how to do this? (I haven't actually tried it because I haven't gone home)



Report Offensive Message For Removal


Response Number 1
Name: borelli34
Date: March 10, 2003 at 23:43:31 Pacific
Reply: (edit)

The preferred method will differ depending upon what compiler and version you are using.

borelli34


Report Offensive Follow Up For Removal

Response Number 2
Name: scott
Date: March 10, 2003 at 23:57:38 Pacific
Reply: (edit)

I just want to know the syntax. it would work for all c++ regardless. it's just a simple console application. how can I have a user enter a string, and use that string to choose the file to take input from?


Report Offensive Follow Up For Removal

Response Number 3
Name: Don Arnett
Date: March 11, 2003 at 11:06:50 Pacific
Reply: (edit)

I'm a C programmer who has learned C++ but has never converted to using C++ I/O (cin/cout). I continue to use C I/O (scanf(), fgets(), printf(), etc), so I'm not well versed in cin syntax.

But, basically you just want to let the user input a string, which I would imagine that you've done before, then pass that string to the open method.

char filename[50];

cin >> filename; // remember I don't use cin so adjust this syntax if it's wrong

input.open(filename);

It should be as easy as that.


Report Offensive Follow Up For Removal

Response Number 4
Name: scott
Date: March 13, 2003 at 10:30:29 Pacific
Reply: (edit)

you're right. i did it, and it works. thanks


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software