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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

user specified input file in c++

Reply to Message Icon

Name: scott
Date: March 10, 2003 at 21:31:05 Pacific
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)




Sponsored Link
Ads by Google

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

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

borelli34


0

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

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?


0

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

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.


0

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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


Sponsored links

Ads by Google


Results for: user specified input file in c++

Reading files in c++ www.computing.net/answers/programming/reading-files-in-c/9024.html

Problem reading file in c++ www.computing.net/answers/programming/problem-reading-file-in-c/7826.html

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html