| | |
Inputting values from a different text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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 :)
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.
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
Something along those lines. Untested i don't have access to compilers in college
Chris
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)
#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
Last edited by Freaky_Chris; Oct 15th, 2008 at 5:30 am.
Knowledge is power -- But experience is everything
The while loop won't work properly, eof is unsafe to use in looping.
This is better:
This is better:
C++ Syntax (Toggle Plain Text)
while (getline(locationtemp, line)){ numbers[x++] = strtod(line); }
•
•
•
•
ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!
Chris
FILE.txt
My
name
is
//end
//screen output
My
name
is
is
//end
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
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?
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?
![]() |
Similar Threads
- Counting lines in a text file and further operations (C++)
- need help finding a script or tool to use for a news update program. (PHP)
- Need help please?? (Java)
- Writing stuff inside files in C++ bloodshed (C++)
- Help with a 2D array from a text file (C++)
Other Threads in the C++ Forum
- Previous Thread: Two dimensional array
- Next Thread: taking code and changing format to function! PLEASE HELP!!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






