Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get contents of notepad file? 8

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
581
PH
Hi everyone… i have 1,12,F,U as contents of my notepad, does filetostr() function can get the content in separate value (as separated by coma) not as “1,12,F,U” Thanks and Godbless…
 
Hi Mandy

You could use m.mystring = filetostr("c:\myfile.txt") to read the whole text file in, then process each line using a loop:
Code:
m.mystring = filetostr("c:\myfile.txt")
for i = 1 to memlines(m.mystring)
  m.myline = mline(m.mystring,i)
  for x = 1 to getwordcount(m.myline,",")
    ? getwordnum(m.myline,x,",")
  next x
next

This would be good for small text files, but would get slow quite quickly.

If you knew the breakdown of each line in advance, you could create a table with the right structure
and use append from ("c:\myfile.txt") csv delimited with , to get the file parsed.

You could also create a table with a single large text field and append the text in there.
Then scan that table to break out the entries as above.

The code above could also be tweaked by storing the memlines result in a variable, so the number of lines is not
counted over and over...


Many ways to 'skin a cat'


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Hello Mandy. We haven't seen you for a while. I trust you are busy.

FILETOSTR() simply copies the entire contents of the file into a single variable. It makes no difference what the file actually contains. So it won't take any special action with regard to the commas.

If you have a file where the values are separated with commas, and you want to get each of those values separately, then use FILETOSTR() to get the whole thing into a variable, and then use ALINES() to get the separate values into an array.

Here's an example:

Code:
lcString = FILETOSTRING("MyFile.txt")
ALINES(laValues, lcString, 0, ",")
* The array laValues will now ocntain the individual values

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Griff and Mike… yep i have been busy in the past months.. but i missed asking and having convo with you experts…

Thanks to both of you, maybe ill try both suggestions… Godbless…
 
[pre] 1,12,F,U[/pre]
Data stored like that is called CSV - short for comma separated values. Especially with many lines all with the same number of values and commas.

You can use [tt]APPEND FROM filename TYPE CSV[/tt] for reading them into a DBF, the DBF just has to have 4 fields in this case and must be used and selected before the APPEND. That's the straight forward way to read in CSV files. By the way, the file extension for such files is often TXT and not specifically CSV, also to support simple browsing of the file content with notepad. It's nevertheless neither a reason to edit them with notepad, to consider notepad as the mainly associated application, to consider them as text, nor to treat them with FILETOSTR, despite what Mike said is correct, FILETOSTR() reads in any file below 2GB size and can be used for many things, but it does not interpret a file as it should be by its file type, it just reads a file into a string as the function name says, no more, no less.

Chriss
 
Mandy,

While we are always happy to help with this sort of question, do keep in mind that it would have been easy for you to find the answer yourself. You already knew about FLETOSTR(). All you had to do was to execute it in the command window and then display the result on the screen, using the ? command. That way, you would have seen instantly how the function works.

Of course, not all questions are so easily answered, so do come back if you run into more difficulties.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank you so much Chriss… yes Mike ive done ?filetostr() to test… thank you always for helping… Godbless…
 
Mandy--

Note that the GETWORDNUM() and GETWORDCOUNT() functions do not always provide the expected results. For example, if the line being parsed is as follows:

TEXT1,TEXT2,,TEXT4

You would expect that there are four possible values with the third value being blank. However, these functions will ignore the ',,' as a possible third value and instead return TEXT4 ss the third value.

Greg
 
Beyond the unexpected behavior with GetWordCount()/GetWordNum(), ALINES() is faster. It doesn't matter much when the data is small, but the larger the string you pass, the larger the difference.

ALINES() is simply wickedly fast, regardless of what you hand it, while pretty much every other way of parsing a string gets bogged down as the strings get bigger.

Bottom line: any time you actually need to parse the whole string and it's regular, ALINES() should be your first thought.

Tamar
 
Hi everyone... It worked! I got the words from my notepad... another question... what if i have Crow, Mandy in my editbox and i need to separate mandy from crow thru a coma? what command will i use? is it filetostr() also or vfp has a code specific in reading editbox and saparating words using coma? Thanks...

can i use also this?

m.mystring = ("Crow" + "," + "Mandy")
for i = 1 to memlines(m.mystring)
m.myline = mline(m.mystring,i)
for x = 1 to getwordcount(m.myline,",")
? getwordnum(m.myline,x,",")
next x
next

Although i got the output i needed, is there another way?
 
Code:
m.myline = thisform.myeditbox.value
for x = 1 to getwordcount(m.myline,",")
    messagebox( getwordnum(m.myline,x,","),48,str(x))
next x

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

There is no place like G28 X0 Y0 Z0
 
Mandy, FILETOSTR, is for reading files, it's neither for splitting something, nor for reading from controls, so how could that help with an editbox.value?

That's obviously a completely wrong idea. 'Did you not get by now that FILETOSTR() is doing exactly what it's named by? It reads a file (in)to a string, FILE/TO/STR.

Now to the actual solution:
You have been provided the idea to split comma separated values by GETWORDNUM, that's working with that problem, you just have to use the editbox.value as the string to process or first copy that into a string variable, meaning simply stringvar = editbox.value.

Chriss
 
Yes Chriss... just afraid to try.. i thought there is a specific vfp command for extracting from string... Thanks Griff and Chriss....
 
Mandy Crow said:
i thought there is a specific vfp command for extracting from string
GETWORDNUM is exactlya that, it is a function that gets a word from a string, so it extracts part of a string from a longer string. Usually it splits by spaces, as that's the normal dellimiter of words, but it has athe paramater to choose splitting by other characters, not only comma.

I also don't get why you tried MEMLINES. That splits lines. If you only have a single line, you don't need MEMLINES.

There is a command for extracting somethng from a string, you just have to combine those words in the right order and abbreaviation, it's STREXTRACT, shortened from STRINGEXTRACT, so you see a lot of functions shorten STRING TO STR, likje FILTOSTR, STREXTRACT or also the STR() function, it's a common abbreviation used for strings in the naming of functions.

STREXTRACTs use case differs from what you want to do, though. You would use that if you know a delimiter before the part to extract from a string and a delimiter after what you want to extract, called the BeginDelimiter and EndDelimiter, for the case "part1,part2" the comma is the end delimiter for part1, but part1 has no begin delimiter. For part2 the comma is the begin delimiter, but it has no end delimiter. So just because it is a function to extract a part of a string, it's not very useful for this case and just because it's a function extracting a string it's not usable for all times you want a part of a string.

There are three more string functions that extract from a string, LEFT, RIGHT and SUBSTR. Are they appropriate for the case of "part1,part2"? LEFT could get the left part, RIGHT could get the right part. Why don't we use them for this and use GETWORDNUM for this exact case, Mandy? That's a homework question. The hint to solving that is: Look at the definitions of the functions and what parameters they have and how useful they are for the case you want to solve. For the general case of a string with two parts separated by a comma, the position of the comma isn't fixed, because part1 and part2 can have any length. Another hint is: You could find out the position and then make use of LEFT and RIGHT or LEFT and SUBSTR. But that's overcomplicating things, as we haqve the GETWORDNUM function at hand, which - look up the definition and parameter description - gives us the straight forward set of parameters to split at a comma, no matter where exactly it is, so it's the best to use that.

How do we get there and know what to use? Experience and knowledge. You can't program, if you need to lookup something every 5 seconds, so you have to know your string functions like you know your abc and multiplication tables. What you have to do is learn more about the plethora of functions and commands and don't guess what#s perhaps working, know what's right for the case at hand.

MEMLINES would become useful again, if your editbox would contain many lines of "lastname, firstname" sequences, likewise ALINES could be used, though. And if such data is in a file thats a use case not for FILETOSTR followed by ALINES and GETWORNUM, it's a case for APPEND FROM filename TYPE CSV, because that does all that in one command. And while that's so compact in comparison of the need of using ALINES an da loop of GETWORNUM, it's not the best idea to create a file from a string to be able to use APPEND. Because creating files is using a slow medium of a hard drive, in comparison to doing things in RAM memory. So just because you already would know STREXTRACT or APPEND you don't use it for any string extraction or splitting, you use something that's appropriate for the current case.

And I want to conclude in giving the last advice about that: If you simply your problem case too much, i.e. ask how to split "Crow,Mandy" when the problem at hand would be splitting a list of names. You only get a good solution provided you give a good explanation of the problem. And that also works for yourself in making you aware of what could be a solution. Think MEMLINES with LINES in it when you have one line? Think again, Mandy.

Chriss
 
I just want to add to Chriss's detailed answer that anytime you want to break up something that's regular, try ALINES() first. So, for example, if you have a line of data separated by commas, use ALINES() and specify "," as the separator. There are times you need StrExtract() because you're pulling out data between specific delimiters that aren't the same, but when it's the same, use ALINES().

I keep mentioning this because ALINES() is so much faster than everything else.

Tamar
 
Oh... Thank you Tamar... although ive used what griff have suggested and its working... i would like to study all that has been suggested, thanks to Griff, Ggreen,Mike,Chriss.. I really wanted to have the words in my editbox separately (by coma) and append it to my databse... i have these codes...

cmpltname = ALLTRIM(SUBSTR(ALLTRIM(thisform.pageframe1.page1.edit1.Value),5,120))

SELECT tsulet
SET ORDER TO mobile

m.mystring = (cmpltname)

for i = 1 to memlines(m.mystring)
m.myline = mline(m.mystring,i)
for x = 1 to getwordcount(m.myline,",")

DO case
CASE x = 1
fnamex = getwordnum(m.myline,x,",")
CASE x = 2
snamex = getwordnum(m.myline,x,",")
CASE x = 3
sexx = getwordnum(m.myline,x,",")
CASE x = 4
gradex = getwordnum(m.myline,x,",")
CASE x = 5
bdayx = getwordnum(m.myline,x,",")
ENDCASE
next x
next

IF FLOCK()
APPEND BLANK
REPLACE idnum WITH thisform.pageframe1.page1.text1.value;
fname WITH PROPER(ALLTRIM(fnamex));​
sname WITH PROPER(ALLTRIM(snamex));​
sex WITH UPPER(sexx);​
mobile WITH sender;​
ay WITH ALLTRIM(STR(YEAR(DATE())-1));​
grade WITH ALLTRIM(STR(VAL(gradex) -1));
dobirth WITH CTOD(bdayx);​
status WITH "Pre-Reg";​
mode WITH "B";​
IN tsulet​
ENDIF
UNLOCK IN tsulet​
 
Mandy, I wonder why you did you say "another question" when it still was about the editbox containing multiple lines of each 5 comma separated values?

To process such an editbox content it's easiest to use APPEND. All your code can be siplified to one APPEND, after you store the editbox content to a file. It also makes me wonder if you read in the file with FILETOSTR to display it in the editbox, then you even already have the file.

All you need besically is:
Code:
SELECT tsulet
APPEND FROM filename TYPE CSV

I see from your REPLACE the fields don't match the full tsulet structure, so you have to make use of the fields clause to specify the fields to append into.

Code:
SELECT tsulet
APPEND FROM filename FIELDS fname, sname, sex, grade, dobirth TYPE CSV

But as fine as all the exercising with string functions is, if you have comma separateed values in multiple lines that is called a CSV file and you have APPEND for this.
After you appended the data, you can add in the other field values into the new records, like the status, mode. If reading in the dobirth fails, then that's because the date formatting in the text files doesn't match your current date format settings, that may have to be addressed, too. But the major work is done in this two lines with far less effort.

Chriss
 
To start over again, when you have files delivered to you or as output from somewhere which contain lines of comma separated values you have APPEND to process them.
Forget about FILETOSTR in such situations, and GETWORDNUM, MEMLINES, ALINES, etc. These all become unimportant in that case.

CSV data is a common data exchange format, and while it's not as standardized as other text based data formats like XML or json, a databsae language like VFP has features for coping with them. So that's where your question and problem description should have started with. It did, kind of, as you talked about notepad displaying 1,12,F,U. Well, there was much more in it, wasn't it? You're hindering solving the actual problem by only telling one little detail. That leads to such lengthy discussions, where you could just have used APPEND FROM file.

And I already told you about it in my first post, just look at the post by me from 18 Jun 24 08:20:
myself said:
1,12,F,U

Data stored like that is called CSV - short for comma separated values. Especially with many lines all with the same number of values and commas.

You can use APPEND FROM filename TYPE CSV for reading them into a DBF, the DBF just has to have 4 fields in this case and must be used and selected before the APPEND.

Chriss
 
And to give you a simple example, store this into a txt file you store into c:\data\people.txt:
Code:
Mandy,Crow,f
Tamar,Granor,f
Mike,Lewis,m
Chris,Miller,m
Griff,MG,m

Then run this code:

Code:
Create Cursor import (id int autoinc, lname c(20), fname c(20), sex C(1))
Append From ("c:\data\people.txt") FIELDS fname,lname,sex TYPE CSV

Browse
You see why I asked to store the text into c:\data\people.txt, you could also store it anywhere else and adapt the code correspondingly. APPEND, like every other file related command, would also find the file "people.txt" without a path, if it's in the current directory.

This demonstrates several things.

First, of course, one command - APPEND - reads the file, splits the values at the commas (aka CSV) and stores it into a table or cursor.
I explicitly made it a bit complicated to not have the fields in the same order as the CSV is. The field clause helps with that and enables specifying the fields in the order of appearance in the CSV. Then I also skip the id field in the fields list, as that's not included in the CSV, and let the autoinc do its job.

Everything else is aftermath from there, if you need the sex (m/f) as big letters, just replace sex with upper(sex), etc. And, of course, if you import into such a cursor and want part of the data in another table, you have all of xbase and sql commands to copy over the data into the right places in your DBFs. But you see it spares a lot of detail work on splittin lines and strings, you don't need to go into that detail level when you use APPEND from a text file. There's a second syntax of APPEND using TYPE DELIMITED instead of CSV. It allows to specify other characters than commas, if you would have a file with other separators like the pipe symbol |, which also sometimes is used.



Chriss
 
Hi Chriss thanks for explaining comprehesively... especially on the dobirth... i have never seen that coming... also sorry Chriss i was not hindering solving the actual problem. In my first application i really needed to read from the notepad... and when i have accomplished this thru your help, i want more, so i wonder and thought of extracting data this time from an editbox and put it the database... and used the same method or answer suggested... trying to explore more to learn from all you experts... sorry Chriss.... thank you for all you explanation i am learning alot and want to learn more... Thank you and God bless....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top