read from input file name !! :/

hi,

How to read from a file when user is asked to enter the file name
for example, the program will prompt

enter filename you wish to see,

and user enter file name and content of that file will show..i know i have to use ifstream but how to link with userinput filename and the file.
help..and thank you. Sorry im just a beginner
If you've learned c-strings only, it should be clear. You can read a filename to a char array and what ifstream::open wants from you is a char array as well, so just pass what you have.
If you use std::strings, it's not so clear. First read the filename, then use std::string::c_str() method to convert std::string to char* and pass what you get to fstream::open.
If it didn't get any more clear, post how you read ac string and how you open a file (unrelated operations). Then it will be more clear what your problem is.

how to make the program reads a list of students marks from an input file,the name of the file from the user.
(the list of students marks should be created by me)

each line in the input file contains a student number followed by marks in test (out of 10) separated by a tab

my list for e.g :
812 8
823 5
938 9
767 7
768 6


my c++ program:

ifstream infile;
string filename;

cout << "Enter the file name: " << endl;
cin >> filename;
infile.open(filename.c_str());


...?


then what to do to read the list that I create ?? please help me :/
then you write
1
2
3
4
5
int no, mark;
while( infile >> no >> mark ) {
   /*put no and mark into some sort of container*/
   /*or do whatever else you want with them*/
}

why to use while !!


Where can I put the list of marks ?
while, because you have many lines in your file that need to be read (until you reach the end of file).

You can put the list of marks into any container (if you know how to use them) or simple array.

aha , I've used array

for(int x=0;x<5;x++)
infile >> ID >> homew;


but I get an error :
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int [5]' (or there is no acceptable conversion)

how to solve it ?
Last edited on
ID is an array ID[x] is a integer. >> reads integers, not arrays. Same for homew (I am guessing)

yes you are right , then how to change ID into integer ?
I just wrote it. ID[x]


thanks :>
Topic archived. No new replies allowed.