-
July 25th, 2012, 10:25 AM
#1
reading data character by character from text file
doubles values are stored in text file. 23.5 36.8 34.2 my teacher has advised me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi() i have to convert it into double. but i dont know how to do this could anyone plz explain with some code relevant to my example. im new to filing so pardon me for basic questioning
-
July 25th, 2012, 10:38 AM
#2
Re: reading data character by character from text file
Can be done, but why?
Why not just read the floating values:
Code:
std::ifstream input("test.txt");
double d;
while (input >> d) {
std::cout << "Read " << d << "\n";
}
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
July 25th, 2012, 10:51 AM
#3
Re: reading data character by character from text file
following errors incured
error C2065: 'input' : undeclared identifier
E:\MyProjects\practice\practice.cpp(54) : error C2296: '>>' : illegal, left operand has type 'double'
E:\MyProjects\practice\practice.cpp(54) : error C2297: '>>' : illegal, right operand has type 'double'
Last edited by tspga; July 25th, 2012 at 10:57 AM.
-
July 25th, 2012, 10:52 AM
#4
Re: reading data character by character from text file
It would help if you showed us the code too.
-
July 25th, 2012, 10:58 AM
#5
Re: reading data character by character from text file
double d;
//char ch;
ifstream file("E:\\abc.txt");
if(!file)
{
cout<<"error";
exit(1);
}
while(input>>d)
{
std::cout<<d;
}
file.close();
getch();
-
July 25th, 2012, 11:02 AM
#6
Re: reading data character by character from text file
You declared your ifstream to be named file, then you used the name input. You should be consistent.
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
|