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++.
Printable View
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++.
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.
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.
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
Of course, you can read them as numbers, not strings: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;
}
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;
}
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:Quote:
Originally Posted by grabbler
However, I put that for only to prove that the file is read ok. That was not the issue here...Code:for(std::vector<std::string>::const_iterator ctit = numbers.begin();
ctit != numbers.end(); ++ctit)
std::cout << *ctit << '\n';