cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to read a text file

Occasional 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
Highlighted
Community Hero

Re: Unable to read a text file

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

Re: Unable to read a text file

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?

Community Hero

Re: Unable to read a text 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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: Unable to read a text file

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)

 
  }

Community Manager

Re: Unable to read a text file

Thanks for your help, guys!

 

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

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓