-
April 8th, 2008, 02:51 PM
#1
Open File and Read
Hey guys I'm having trouble figuring this out.
I'm use fstream to open the file...
Now how can I get my program to read data from the file and store them into variables?
Code:
for (;;)
{
fstream filestr;
filestr.open (File);
// Skip leading white space
double G = 0, H = 0, L, Item;
//PROBLEM STARTS HERE
cin >> ws;
if (cin.eof()) break;
// Look ahead at next character in the input stream (without consuming it)
if ( isdigit( cin.peek() ) )
{
// Next character was a numeric digit, so input a real number
cin >> D;
cout << "Number: " << D << endl;
I'm using Linux right now, a.out < filename works just fine.
But I have to use this format: a.out filename filename2 filename3.
Do I even use cin?
Thank you.
-
April 8th, 2008, 03:11 PM
#2
Re: Open File and Read
OK, when you are reading in from a file you do not use cin. Instead you use the fstream variable name. So using your code:
Code:
fstream filestr;
filestr >> ws;
I hope this helps.
-
April 9th, 2008, 12:16 PM
#3
Re: Open File and Read
When using a command line like this:
it loads the file in using cin (only on Linux too, this won't work on Windows), but like this:
You have to use command line arguments. So in your main function:
Code:
int main(int argc, char * argv[]){
for (int i = 1; i < argc; i++){
ifstream file(argv[i]);
//process each file here
}
}
argv is an array of all the arguments to your program. argv[0] is the name of your program (in the example it would be "a.out") and the rest would be any other arguments you passed. The argc variable tells you how many arguments were passed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|