Inputting values from a different text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 4
Reputation: cloudii is an unknown quantity at this point 
Solved Threads: 0
cloudii cloudii is offline Offline
Newbie Poster

Inputting values from a different text file

 
0
  #1
Oct 15th, 2008
Hi, I'm learning intro to c++.

My question is, how do I input values that come from a different text file so that the program uses those values for calculations?

Basically, it gives me a list of numbers and tells me what the numbers are for... and then I have to integrate them using ifstream.

For example..
Text file:
111
222
333
444

555
666

and each of those lines represent a variable,
a
b
c
d

e
f

(notice the space in between d and e, does it change anything?)
I got so far as to opening the file correctly... but I'm stuck.
__________

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main() {

char myFileName[64];
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
double f = 0;
ifstream locationtemp;

cout << "Enter the file name.\n";
cin >> myFileName;

locationtemp.open(myFileName);

if ( locationtemp.fail() )
{
cerr << "\n\nERROR:Unable to open file/File did not open correctly\n";
return(1);
}


___________

btw those arent the real values or variables.

Any help is greatly appreciated. Thanks :)
Last edited by cloudii; Oct 15th, 2008 at 4:32 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Inputting values from a different text file

 
0
  #2
Oct 15th, 2008
What is numberObs?
And I don't get what do you want exactly?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: cloudii is an unknown quantity at this point 
Solved Threads: 0
cloudii cloudii is offline Offline
Newbie Poster

Re: Inputting values from a different text file

 
0
  #3
Oct 15th, 2008
oops sorry it's supposed to be locationtemp

my question was how do I use values given in a text document (the "myFileName") and input them into my program variables so they can be used for calculations?
Last edited by cloudii; Oct 15th, 2008 at 4:35 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Inputting values from a different text file

 
0
  #4
Oct 15th, 2008
Just use locationtemp as you would use cin.
Of course, that is if your file contains only numbers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: cloudii is an unknown quantity at this point 
Solved Threads: 0
cloudii cloudii is offline Offline
Newbie Poster

Re: Inputting values from a different text file

 
0
  #5
Oct 15th, 2008
yes it does contain only numbers. so i would have to make a new variable? for example

int number = 0;
(...)

locationtemp >> number;

?

but how would I know which variables each number will be assigned to? am i able to specify it using 'line' or something?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Inputting values from a different text file

 
0
  #6
Oct 15th, 2008
The file will be read line by line. So the first line would be stored in the first variable. Ofcourse, if you want to make it possible to loop throught the file until all the variables are read in then your best bet would be to use and array (not stricktly true, vectors would be better but don't worry about those yet).

so create an array of doubles then loop throught the file storing each line into the next cell in the array.

i would do something like this

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ifstream>
  5.  
  6. int main(void){
  7. char myFileName[64];
  8. string line;
  9. double numbers[100] = {0};
  10. int x = 0;
  11.  
  12. ifstream locationtemp;
  13.  
  14. cout << "Enter the file name.\n";
  15. cin >> myFileName;
  16.  
  17.  
  18. locationtemp.open(myFileName);
  19. if ( !locationtemp.good() ){
  20. cerr << "\n\nERROR:Unable to open file/File did not open correctly\n";
  21. cin.get();
  22. return(1);
  23. }
  24.  
  25. while( !locationtemp.eof() ){
  26. getline(locationtemp, line);
  27. numbers[x++] = strtod(line);
  28. }
  29. locationtemp.close();
  30. return 0;
  31. }

Something along those lines. Untested i don't have access to compilers in college

Chris
Last edited by Freaky_Chris; Oct 15th, 2008 at 5:30 am.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Inputting values from a different text file

 
1
  #7
Oct 15th, 2008
The while loop won't work properly, eof is unsafe to use in looping.
This is better:
  1. while (getline(locationtemp, line)){
  2. numbers[x++] = strtod(line);
  3. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Inputting values from a different text file

 
0
  #8
Oct 15th, 2008
ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Inputting values from a different text file

 
0
  #9
Oct 15th, 2008
Originally Posted by Freaky_Chris View Post
ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris
Furthermore, even if you reach end of file, eof won't be set to true until you try to read after the EOF. That will cause this kind of error:
FILE.txt
My
name
is
//end

//screen output
My
name
is
is
//end
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: cloudii is an unknown quantity at this point 
Solved Threads: 0
cloudii cloudii is offline Offline
Newbie Poster

Re: Inputting values from a different text file

 
0
  #10
Oct 15th, 2008
Thanks for all the help.
I'm still confused though, is there a good site that explains the meaning of codes well for beginners?

I still don't get how the values get assigned in order. Will I have to list the variables in order at the beginning?
__________

int main () {

char filename[64];
int presentTemp = 0; // first number from text
double desiredTemp = 0; // second
double timeInterval = 0; // third
double toleranceOn = 0; // fourth
double toleranceOff = 0; // fifth

double changeInTemp = 0; // from sixth value onwards (to the end of file)

ifstream ccLocation;

cout << "Enter the file name.\n";
cin >> myFileName;

ccLocation.open(filename);
if ( ccLocation.fail() ){
cerr << "\n\nERROR:Unable to open file\n";
cin.get();
return(1);
}

while( getline(ccLocation , line) ){
getline(ccLocation, line);
numbers[x++] = strtod(line);
}

ccLocation.close();

__________

would that be correct?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC