| | |
Help me Convert Function to C++
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
Private Function IsTextFile(FileName As String) As Boolean Dim FF As Integer Dim FileData() As Byte Dim K As Long FF = FreeFile Open FileName For Binary Access Read As FF ReDim FileData(LOF(FF) - 1) Get FF, , FileData Close FF For K = 0 To UBound(FileData) If FileData(K) > 126 Then IsTextFile = False Exit For End If Next K If K = UBound(FileData) + 1 Then IsTextFile = True End Function
Please help me
Thanks
•
•
Join Date: Jul 2005
Posts: 1,881
Reputation:
Solved Threads: 298
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)
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: 1) declare variables: bool result = true; int K = 0; vector<int> FileData ifstream FF(FileName.c_str()); 2) read data from file into FileData while(FF >> K) FileData.push_back(K); FF.close(); 3) evaluate data for(K = 0; K < FileData.length; ++K) if(FileData[K] > 126) result = false; 4) return result
Last edited by Lerner; May 8th, 2008 at 5:37 pm.
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.
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.
So I would think this would work. Note: the following is untested and not compiled.
C++ Syntax (Toggle Plain Text)
bool IsTextFile(string FileName) { bool isText = true; // open the file in binary mode ifstream in( FileName , ios::binary); if( in.is_open() ) { size_t len; // locate end of file in.seekg(0, ios::end); // get file length len = in.tellg(); // back to beginning of the file in.seekg(0,ios::begin); // allocate buffer space unsigned char* buf = new [size+1]; // read the file into memory in.read(static_cast<char*>(buf), size); in.close(); // now check if its a text file for(size_t i = 0; i < size && isTest == true; i++) { if( buf[i] < 13 || buf[i] > 126) { isText = false; } } delete[] buf; return isText; }
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Similar Threads
- Using Function Help (C++)
- convert int to string (C)
- how to convert Right function to C# ? (VB.NET)
- creating our own C++ strcat() function, code reqd (C++)
- Function templates question (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
- Need help to convert Int64 to Base36 ? (PHP)
- VC++:convert File IStream to hex and store...? (C++)
Other Threads in the C++ Forum
- Previous Thread: grade()...Help
- Next Thread: recursive binary search tree
Views: 1197 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm api array arrays assignment beginner binary borland browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee ifstream input int java lazy lib library linker list loop looping loops math matrix memory newbie news number object objects opengl output path pointer pointers problem program programming project random read reading recursion recursive reference simple sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock






