CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: indexed buffer

  1. #1
    Join Date
    Jul 2005
    Posts
    20

    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++.

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  3. #3
    Join Date
    May 2005
    Posts
    99

    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.

  4. #4
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    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)

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    May 2005
    Posts
    47

    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.


  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: indexed buffer

    Quote 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...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured