943,657 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1056
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 15th, 2008
0

Inputting values from a different text file

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cloudii is offline Offline
4 posts
since Oct 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

What is numberObs?
And I don't get what do you want exactly?
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cloudii is offline Offline
4 posts
since Oct 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

Just use locationtemp as you would use cin.
Of course, that is if your file contains only numbers.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cloudii is offline Offline
4 posts
since Oct 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 15th, 2008
1

Re: Inputting values from a different text file

The while loop won't work properly, eof is unsafe to use in looping.
This is better:
C++ Syntax (Toggle Plain Text)
  1. while (getline(locationtemp, line)){
  2. numbers[x++] = strtod(line);
  3. }
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 15th, 2008
0

Re: Inputting values from a different text file

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cloudii is offline Offline
4 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Two dimensional array
Next Thread in C++ Forum Timeline: taking code and changing format to function! PLEASE HELP!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC