|
-
July 31st, 2005, 10:25 PM
#1
indexed buffer
i need to get the elements of a file,elements are separeted by tab.
suppose myfile.txt
2.389 .809 .320 . .
.
.
i need to get it into
like
b[1][]=2.389
b[2][]=..809
b[1][]=.320
.
.
.
etc
plz help me by sending code in c++.
-
July 31st, 2005, 10:29 PM
#2
Re: indexed buffer
Sorry abul, but we don't do homework here. We could only help if you can show us the code that you have written and tell us what diffficulties you are facing.
-
July 31st, 2005, 10:33 PM
#3
Re: indexed buffer
Do it yourself buddy :-) It will help you.
Try to write a small program and find out the errors.
Hint is try to read the line and look for spaces.
-
July 31st, 2005, 10:49 PM
#4
Re: indexed buffer
I may only suggest you to use somefunctions like
Code:
bool IsSpace(char a)
{
if(a==' ')
return true;
else
return false;
}
// and then use while cycle
God could improve essentially a
human nature, but he
was too anxious with compatibility
with the monkey.
(Eugeny Goldberg)
-
August 1st, 2005, 02:58 AM
#5
Re: indexed buffer
Code:
int main(int argc, char* argv[])
{
std::vector<std::string> numbers;
std::ifstream file;
file.open("d:\\data.txt");
if(!file.is_open())
return -1;
std::string sval;
while(file >> sval)
numbers.push_back(sval);
file.close();
for(std::vector<std::string>::const_iterator ctit = numbers.begin();
ctit != numbers.end(); ++ctit)
std::cout << *ctit << std::endl;
return 0;
}
Of course, you can read them as numbers, not strings:
Code:
int main(int argc, char* argv[])
{
std::vector<float> numbers;
std::ifstream file;
file.open("d:\\data.txt");
if(!file.is_open())
return -1;
float fval;
while(file >> fval)
numbers.push_back(fval);
file.close();
for(std::vector<float>::const_iterator ctit = numbers.begin();
ctit != numbers.end(); ++ctit)
std::cout << *ctit << std::endl;
return 0;
}
-
August 1st, 2005, 11:49 AM
#6
Re: indexed buffer
just a note that might be ignored though taht endl shouldnl be called all teh time like taht or it's gonna slow down the program if the file is too large.
-
August 1st, 2005, 02:01 PM
#7
Re: indexed buffer
 Originally Posted by grabbler
just a note that might be ignored though taht endl shouldnl be called all teh time like taht or it's gonna slow down the program if the file is too large.
Yes, that is true. std::endl flushes the buffers. And it would be better to use:
Code:
for(std::vector<std::string>::const_iterator ctit = numbers.begin();
ctit != numbers.end(); ++ctit)
std::cout << *ctit << '\n';
However, I put that for only to prove that the file is read ok. That was not the issue here...
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
|