Forum Discussion

komalak11's avatar
komalak11
Contributor
6 years ago

Unable to read a text file

Hi,

 

I am trying to read a text file placed on the desktop using the following piece of code. Unfortunately, the text which is read is gibberish. Can someone please help me out how to read text files correctly. 

 

And how to work the the content of the text file, like finding strings?

 

sPath ="C:\\Users\\INKKANAM\\Desktop\\API_Error.txt";

myFile = aqFile["OpenTextFile"](sPath, aqFile.faRead,aqFile.ctUnicode);

Log["Message"]("File read");

var sFile = myFile["ReadAll"]();

Log["Message"](sFile);

myFile["Close"]();

 

Output: See attached picture

 

 

5 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Just to double-check: are you sure that the file is really in Unicode format, but not in, say, UTF-8 one?

     

    • komalak11's avatar
      komalak11
      Contributor

      Hi,

       

      Yes I changed the Unicode format.

       

      Now that has been resolved, how to access the content of this file and search for a word within this file?

      • anupamchampati's avatar
        anupamchampati
        Frequent Contributor

        Open file in Notepad and save in unicode or whatever format you are using in TextCoding Type

        aqFile.ctANSI 20 ANSI
        aqFile.ctUnicode 21 Unicode (UTF-16)
        aqFile.ctUTF8 22 UTF8

         

        2. for searching in particular line use

        var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);

        while(! myFile.IsEndOfFile())
          {
            s = myFile.ReadLine();
            ////use :aqstring.find

        aqString.Find(InputStringSubStringStartPositionCaseSensitive)

         
          }