Help me Convert Function to C++

Reply

Join Date: May 2008
Posts: 1
Reputation: iammop is an unknown quantity at this point 
Solved Threads: 0
iammop iammop is offline Offline
Newbie Poster

Help me Convert Function to C++

 
0
  #1
May 8th, 2008
  1. Private Function IsTextFile(FileName As String) As Boolean
  2. Dim FF As Integer
  3. Dim FileData() As Byte
  4. Dim K As Long
  5.  
  6. FF = FreeFile
  7. Open FileName For Binary Access Read As FF
  8. ReDim FileData(LOF(FF) - 1)
  9. Get FF, , FileData
  10. Close FF
  11.  
  12. For K = 0 To UBound(FileData)
  13. If FileData(K) > 126 Then
  14. IsTextFile = False
  15. Exit For
  16. End If
  17. Next K
  18.  
  19. If K = UBound(FileData) + 1 Then IsTextFile = True
  20. End Function

Please help me
Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,881
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 298
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help me Convert Function to C++

 
0
  #2
May 8th, 2008
I have no idea what language the original is, but here's a guess as to what it might mean in C++ pseudocode:
  1.  
  2. Declare a function called IsTextFile that takes a single argument of type string called FileName and returns type bool. In the body of the function do the following:
  3. 1) declare variables:
  4. bool result = true;
  5. int K = 0;
  6. vector<int> FileData
  7. ifstream FF(FileName.c_str());
  8.  
  9. 2) read data from file into FileData
  10. while(FF >> K)
  11. FileData.push_back(K);
  12. FF.close();
  13.  
  14. 3) evaluate data
  15. for(K = 0; K < FileData.length; ++K)
  16. if(FileData[K] > 126)
  17. result = false;
  18.  
  19. 4) return result
Last edited by Lerner; May 8th, 2008 at 5:37 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Help me Convert Function to C++

 
0
  #3
May 8th, 2008
The original is VB.
Last edited by hammerhead; May 8th, 2008 at 6:01 pm.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,486
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1601
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help me Convert Function to C++

 
0
  #4
May 8th, 2008
I think it opens the file, gets the file length, allocates a buffer if the required size, then checks if any character elements is > 126, which is the upper limit of the text characters in the standard ascii chart.

So I would think this would work. Note: the following is untested and not compiled.
  1. bool IsTextFile(string FileName)
  2. {
  3. bool isText = true;
  4. // open the file in binary mode
  5. ifstream in( FileName , ios::binary);
  6. if( in.is_open() )
  7. {
  8. size_t len;
  9. // locate end of file
  10. in.seekg(0, ios::end);
  11. // get file length
  12. len = in.tellg();
  13. // back to beginning of the file
  14. in.seekg(0,ios::begin);
  15. // allocate buffer space
  16. unsigned char* buf = new [size+1];
  17. // read the file into memory
  18. in.read(static_cast<char*>(buf), size);
  19. in.close();
  20.  
  21. // now check if its a text file
  22. for(size_t i = 0; i < size && isTest == true; i++)
  23. {
  24. if( buf[i] < 13 || buf[i] > 126)
  25. {
  26. isText = false;
  27. }
  28. }
  29. delete[] buf;
  30. return isText;
  31. }

There is another way to accomplish the same thing without allocating any memory. Just read the file one character at a time and test to see if it is a text character or not.
Last edited by Ancient Dragon; May 8th, 2008 at 10:02 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1197 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC