954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inputting values from a different text file

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
#include
#include
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 :)

cloudii
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

What is numberObs?
And I don't get what do you want exactly?

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

oops sorry it's supposed to be locationtemp :S

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?

cloudii
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Just use locationtemp as you would use cin.
Of course, that is if your file contains only numbers.

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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?

cloudii
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

#include <iostream>
#include <string>
#include <cstdlib>
#include <ifstream>

int main(void){
    char myFileName[64];
    string line;
    double numbers[100] = {0};
    int x = 0;
    
    ifstream locationtemp;
    
    cout << "Enter the file name.\n";
    cin >> myFileName;
    
    
    locationtemp.open(myFileName);
    if ( !locationtemp.good() ){
       cerr << "\n\nERROR:Unable to open file/File did not open correctly\n";
       cin.get();
       return(1);
       }
    
    while( !locationtemp.eof() ){
           getline(locationtemp, line);
           numbers[x++] = strtod(line);
           }
    locationtemp.close();
    return 0;
}


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

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

The while loop won't work properly, eof is unsafe to use in looping.
This is better:

while (getline(locationtemp, line)){
         numbers[x++] = strtod(line);
}
Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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?

cloudii
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I would advise you to look at more basic things first, you are jumping ahead.
File manipulation is learned well after loops, arrays and string manipulations... it's not without reason.

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

From the code i originally posted, all you had to do was update the loop to how sci@phy pointed out.

Then understand it....

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You