Ask a Question

Unable to read a text file

komalak11
Contributor

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 pictureFile_Results.PNG

 

 

5 REPLIES 5
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?

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

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?

Hi,

 

The content of the file is in the sFile variable.

You may use methods provided by the aqString object (see help for details) or use regular expressions or any other means that works best for the given file content.

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

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)

 
  }

TanyaYatskovska
SmartBear Alumni (Retired)

Thanks for your help, guys!

 

@komalak11, did these suggestions help you resolve the question? Please mark the best answer as a solution.

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



cancel
Showing results for 
Search instead for 
Did you mean: