How many rows of data in a text file - powerbuilder

I'm new to Powerbuilder and here I have some question about how to find how many rows/lines of data there is in a text file. First I tried a approach of FileLength but could not figure how to to do that with my current logic. Could not find any example of how to do it with it either so here I am wanting some help!
What I'm doing is generating a password from a txt file.
Notes:
gsa_wordlist is a global variable.
Also if you as experienced developer see any common pitfalls then please do tell.
My code on Click():
int li_rand
int li_upperboundList
int li_FileNum
string ls_fileurl
string ls_listout
int i
li_upperboundList = Upperbound(gsa_wordlist)
ls_fileurl = 'C:\Users\abg\Documents\wordlist.txt'
IF li_upperboundList < 100 THEN
Beep(1)
li_FileNum = FileOpen(ls_fileurl)
FOR i = 1 TO // TO HOW MANY LINES OF DATA
FileReadEx(li_FileNum, ls_listout)
gsa_wordlist[i] = ls_listout
NEXT
li_upperboundList = Upperbound(gsa_wordlist)
li_rand = rand(li_upperboundList)
sle_genpass.Text = string(li_rand)
ELSE
Beep(2)
li_rand = rand(li_upperboundList)
sle_genpass.Text = gsa_wordlist[li_rand]
END IF

You don't need to know how many rows, just loop until EOF.
li_FileNum=FileOpen(ls_fileurl,LineMode!,Read!,LockRead!)
i = 1
DO While FileReadEx(li_FileNum,ls_listout) <> -100
gsa_wordlist[i++] = ls_listout
LOOP
FileClose(li_FileNum)
PS. Add a check inside the loop for the li_upperboundList limit !

Related

How to take part from input numbers?

Sorry still learning and new to code. I don’t know if the title is clear or not, I'm trying to get user last and first input digits, I followed some tutorial and changed it to what I need the problem is I get some numbers even if the Edit Text is empty. I don't know if there is another way or not I just followed some topic to get first and last digit. Here is my code .
EditText ca_code = findViewById(R.id.caps_code);
TextView txt_code_out = findViewById(R.id.out_code);
int mutli = 0;
String lst_code = String.valueOf(ca_code);
//char fst = lst_code.charAt(0);
//char snd = lst_code.charAt(1);
char lat = lst_code.charAt(lst_code.length() - 1);
txt_code_out.setText(String.valueOf((int) lat));
for this i get different output if i change length() - 1
for -1 = output = 125 and for -2 =101 , -3=100, -4= 111, -5 =99
Then I changed the code little bit to
EditText capscode = findViewById(R.id.caps_code);
TextView txt_code_out = findViewById(R.id.out_code);
String lst_code = String.valueOf(capscode);
//char fst = lst_code.charAt(0);
//char snd = lst_code.charAt(1);
char lat = lst_code.charAt(lst_code.length() - 1);
txt_code_out.setText(String.valueOf(lat));
The output was }edoc_spac/di:ppa 7 300,,, for -1 to 20
Change to:
String lst_code = capscode.getText().toString();
capscode is an EditText not a number or a String.
With getText().toString() you don't need String.valueOf().
Change the line
String lst_code = String.valueOf(capscode);
to
String lst_code = capscode.getText().toString();
The variable capscode is of type EditText, which has a method getText() to retrieve its current value. The value is from type Editable again and it provides a toString() method finally.

C++ Builder - Copy a Link to a String Based On Certain Words in It

This's my first post here.
I have 4 links in a Memo or TStringList:
http://website.com/text/book&id=20/programming
http://website.com/text/book&id=10/programming
http://website.com/text/book&id=40/programming
http://website.com/text/book&id=30/programming
What I want to do is to copy each link based on its "id number", as shown above to separate string, and NOT based on its
index in TMemo or in TStringList.
This's because the id number of each link is always change every time or dynamics (it's a random id).
So let's say :
if the link containts id=10 then it should be copied to a string called "id10".
final result is id10 containts: http://website.com/text/book&id=10/programming
if the link containts id=30 then it should be copied to a string called "id30".
final result is id30 containts: http://website.com/text/book&id=30/programming
so on and on until all links are copied into a separate different string.
I've tried to resolve this problem using substring function, such as:
int p = 0;
String id10, id20, id30, id40;
p = Memo1->Lines->Text.Pos("id=20");
if (p != 0)
{
id10 = Memo1->Lines->Text.SubString(p + 1, Memo1->Lines->Text.Length());
ShowMessage(id10);
}
......
Unfortunately, its always failed. Because I am new to c++ and the sort but I don't know what to do with all of it. I am sorry.
So I need your help to handle this situation.
Thank you very much.
Calling .Pos() from the text property is getting the index for the position of the found string within the entire text of the memo control. Here is a short example of how to read each line from a TMemo in C++ Builder:
AnsiString id10;
for (int i = 0; i < Memo1->Lines->Count; i++)
{
AnsiString line = Memo1->Lines->Strings[i];
if (line.Pos("id=10") > 0)
{
id10 = line;
ShowMessage(id10);
}
}
Using the above snippet, you should be able to deduce how to make it do what you want.

How to replace data format from Pipe Delimited to Tab Delimited?

Good day. Below is a Powerbuilder script that check the file encoding and get the data from the file. Once the data is assign to a string variable(ls_encoding) it will be passed to the object function, of_TabDelimited() (script of this function was also shown below) to replace the pipe delimited to tab delimited.
While inside the object function of_TabDelimited(), the application got hang-up and I can't figure out the caused of the PB hang-up. The data that was passed is about 2,800 rows. But the function of_TabDelimited() is working fine if the data is not that in bigger rows(e.g. 100 rows). I can't find any limitation of processing in of_TabDelimited() because it only performs a loop and replace function while inside the loop.
Anybody can help me on how to find and correct the error? Appreciate any help.
//Get the data from the file
ll_FileNum = FileOpen(ls_sourcepath, StreamMode!, Read!, LockWrite!, Replace!)
ll_FileLength = FileLength(ls_sourcepath)
eRet = FileEncoding(ls_sourcepath)
IF NOT ISNULL(ll_FileLength) OR ll_FileLength > 0 THEN
IF eRet = EncodingANSI! THEN
ll_bytes = FileReadEx(ll_FileNum, lbl_data)
ls_Encoding = String(lbl_data, EncodingUTF8!)
FileClose(ll_FileNum)
END IF
ELSEIF ISNULL(ll_FileLength) OR ll_FileLength = 0 THEN
lb_Return = FALSE
END IF
Integer li_start, li_end
//Convert the data to TabDelimited
ls_Return = of_TabDelimited(ls_Encoding) //Application hang-up on this function.
IF NOT ISNULL(ls_Return) OR LEN(ls_Return) > 0 THEN
//Parse To Table
END IF
Function : of_TabDelimited
Return Type : String
Argument Type : as_encoding
Long ll_start=1
String ls_old, ls_new
ls_old = "|"
ls_new = "~t"
// Find the first occurrence of old_str.
ll_start = POS(as_encoding, ls_old, ll_start)
// Only enter the loop if you find old_str.
DO WHILE ll_Start > 0
// Replace old_str with new_str.
as_encoding = Replace(as_encoding, ll_start, Len(ls_old), ls_new)
// Find the next occurrence of old_str.
ll_start = POS(as_encoding, ls_old, ll_start + Len(ls_new))
LOOP
RETURN as_encoding
Below is a new function that I've added in the application and tested. And it seems this script is working fine and can process large number of rows. Basically this script was taken from the PFC's function of_GlobalReplace(), from the object n_cst_string. Even the script in of_TabDelimited() was taken from of_GlobalReplace() but the difference is the computation of len() in the variable ls_old and ls_new.
String ls_Source, ls_Old, ls_New
Long ll_Start, ll_Oldlen, ll_Newlen
ls_Old = "|"
ls_New = "~t"
//Script taken from n_cst_string - of_GlobalReplace
//Get the string lenghts
ll_OldLen = Len(ls_Old)
ll_NewLen = Len(ls_New)
//Search for the first occurrence of as_Old
ll_Start = Pos(as_source, ls_Old)
Do While ll_Start > 0
// replace as_Old with as_New
as_Source = Replace(as_Source, ll_Start, ll_OldLen, ls_New)
// find the next occurrence of as_Old
ll_Start = Pos(as_source, ls_Old, (ll_Start + ll_NewLen))
Loop
Return as_Source
I went to review the PFC's of_GlobalReplace() function and copied the script from there but I have removed the checking of case sensitivity. I went to test below function and it is working fine in processing large amount of data from the file.
String ls_Source, ls_Old, ls_New
Long ll_Start, ll_Oldlen, ll_Newlen
ls_Old = "|"
ls_New = "~t"
//Script taken from n_cst_string - of_GlobalReplace
//Get the string lenghts
ll_OldLen = Len(ls_Old)
ll_NewLen = Len(ls_New)
//Search for the first occurrence of as_Old
ll_Start = Pos(as_source, ls_Old)
Do While ll_Start > 0
// replace as_Old with as_New
as_Source = Replace(as_Source, ll_Start, ll_OldLen, ls_New)
// find the next occurrence of as_Old
ll_Start = Pos(as_source, ls_Old, (ll_Start + ll_NewLen))
Loop
Return as_Source

Simple text editing program in C++ [closed]

I got a little bit of trouble creating a program that edits text in C++. Keep in mind that I'm still at the beginning of programing. Here is what I must do:
Some text is placed in text file F. I must write a program-editor, which based on the file F, commands and data from the keyboard creates the file FF. The program-editor must recognize and process the following commands:
AN - insert text after the n-th row;
IN - insert text before the n-th row;
CM,N - substitution of the rows from m-th to n-th;
DM,N - deleting the rows from m-th to n-th;
E - end of editing;
where m and n are the number of rows in the file F.The commands are recorded one on row and are made as a menu.
This is the program. I researched a lot in the web about text editing and there are some text editing programs' source codes, but I guess I'm still in the beginning of programing and I find those source codes really difficult to understand. I'm concerned about few things:
Must I manually put text in the text file F and must I add another option in the menu about adding text;
The other thing is about the commands - how do I find and use the different rows from the text so I can insert, substitute and delete rows;
Well that is all. If you have the time please help me, because I really need to know how this program must be done in a not so complicated way and I think it got some valuable things that I could learn from it. Thanks in advance !
In pseudo-code, you ll find every real function you ll need in the documpentation.:
You ll need to write parse() yourself, all vec.something and input.something are real vector or string function, under a different name, you ll need to search the documentation.
open, close and writeinfile are io function under different name too (and different parameters), again, see the doc
getuserinput is also a renamed basic io function.
The reason I write this is to give you an idea of how to do this, it is not the solution spoon feeded to you, think of it as the algorithm, if you can do your homework without it, it is far better than using it. Also, learn to search the doc, it is really useful
vector<string> vec
int n, m
string input, output
//Open the file
open(your_file)
//Store every line in a string in the vector
while(input != EOF)
{
input = getalinefrom(file)
vec.add(input)
}
//You don t need the file for now, so close it
close(file)
//Create your 'menu', presuming text based, if graphical, well...
do
{
//Get user choice
input = getuserinput()
//Every command is just a letter, so check it to know what to do
if(input.firstchar == 'A')
{
//Parse the input to get n
n = parse(input)
//Get the line to add
input = getuserinput()
//Add it before n
vec.addafter(n, input)
}
else if (input.firstchar == 'I')
{
//Parse the input to get n
n = parse(input)
//Get the line to add
input = getuserinput()
//Add it before n
vec.addbefore(n, input)
}
else if (input.firstchar == 'C')
{
//Well, I don t see what is substitution so I ll let you try
}
else if (input.firstchar == 'D')
{
//Get n and m
n = parse(input)
m = parse(input)
//Presuming n < m, you ll need to check for error
while(n < m)
{
vec.deleterow(n)
n = n + 1
}
}
//Go out of the loop at E since it s the end of the app
}while(input != "E");
//Concatene every line
n = 0
do
{
output = output + vec.contentofrow(n)
}while(n < vec.length)
//Open the file again, with correct flag it will erase it content
open(file)
//Write your new content
writeinfile(file, output)
//Close the file
close(file)
return 0;

Word Interop spellchecking a document word by word is too slow

With the code below basically I'm checking a document word by word and separate the correct ones. I works, but too slow that I have to wait for more than 10 minutes for 40 thousand words. When I open the same document word checks it in a few seconds. What am I doing wrong?
var application = new Application();
application.Visible = false;
Document document = application.Documents.Open("C:\\Users\\hrzafer\\Desktop\\spellcheck.docx");
// Loop through all words in the document.
int count = document.Words.Count;
IList<string> corrects = new List<string>();
for (int i = 1; i <= count; i++)
{
if (document.Words[i].SpellingErrors.Count == 0)
{
string text = document.Words[i].Text;
corrects.Add(text);
}
}
File.WriteAllLines("corrects.txt", corrects);
application.Quit();
a workaround, as it seems you only want the correct words in your document:
for i=ActiveDocument.Range.SpellingErrors.Count to 1 step -1
ActiveDocument.Range.SpellingErrors(i).Delete
next
then save the document with a new name
ActiveDocument.SaveAs FileName:="C:\MyWordDocs\NewFile.docx", _
FileFormat:=wdFormatXMLDocument

Resources