Forum Discussion

santoguya's avatar
santoguya
Contributor
13 years ago

aqFile.ReadWholeTextFile() not reading entire file

I'm using the aqFile.ReadWholeTextFile() function on an input file but the function only grabs about 3-4 lines of the file (I did a Log.Message() of the read text and also a WriteToTextFile() of the read text to verify this). I was successfully able to read the input file by opening the file and doing a "Save" (ctrl + S) and then running ReadWholeTextFile(), but I cannot do this manual open and resave every single time, and the input file is generated by third-party code that I cannot modify.



The file I'm trying to run ReadWholeTextFile() on is attached.

5 Replies

  • ASV's avatar
    ASV
    Contributor
    Hello Randy



    I think to save notepad in this way is not good.

    I suggest this



    Set FSO = Sys.OleObject("Scripting.FileSystemObject")


    Set File = FSO.OpenTextFile("C:\TempFile.txt", 2, True, 0) ' Open for writing

    To open for reading

    write in this way

    Set File = FSO.OpenTextFile("C:\TempFile.txt", 1, True, 0)

    File.readall

     
  • Hi Vahagn,



    Thanks for your input.



    I used your code in my TestComplete script but I'm having trouble calling OpenTextFile fromt the FSO object, I get the following error on the OpenTextFile() line:



    "Microsoft VBScript runtime error - Object doesn't support this property or method."



    I think the FSO object didn't get set properly. I tried using CreateObject("FileSystemObject") which gave me the same error.



    Are there some extra libraries or include files that I need in order to access the FSO methods?



    Thanks!

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Randy,



    aqFile.ReadWholeTextFile actually reads you entire file. For example, if you run:

    var str = aqFile.ReadWholeTextFile("E:\\aertest.txt", aqFile.ctANSI);

    Log.Message(aqString.GetLength(str));
    you'll get 120071, which is exactly your file size.



    The issue with cut-off text is caused by the fact that your file contains NULL control characters (characters with code 0). They are usually used to indicate the end of text, that's why, the text after these characters isn't written to the text log or files created with WriteToTextFile.



    When you open the file in a text editor and then save it, the editor converts NULL characters to spaces, so that the file contents can be properly processed.



    To fix the read text in your TestComplete test in a similar way, you can use the aqString.Replace method to perform the NULL-to-space character replacement:

    var str = aqFile.ReadWholeTextFile("E:\\aertest.txt", aqFile.ctANSI);

    var str2 = aqString.Replace(str, "\0", " ");
  • Thanks Helen! I had to modify it a little for VB but after changing the null character \0 to vbNullChar, it worked perfectly!@
  • Thanks Helen! I had to modify it a little for VB but after changing the null character \0 to vbNullChar, it worked perfectly!@