943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1485
  • C++ RSS
May 8th, 2008
0

Help me Convert Function to C++

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iammop is offline Offline
1 posts
since May 2008
May 8th, 2008
0

Re: Help me Convert Function to C++

I have no idea what language the original is, but here's a guess as to what it might mean in C++ pseudocode:
C++ Syntax (Toggle Plain Text)
  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 6:37 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 8th, 2008
0

Re: Help me Convert Function to C++

The original is VB.
Last edited by hammerhead; May 8th, 2008 at 7:01 pm.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
May 8th, 2008
0

Re: Help me Convert Function to C++

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.
C++ Syntax (Toggle Plain Text)
  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 11:02 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: grade()...Help
Next Thread in C++ Forum Timeline: recursive binary search tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC