Work with read/write file

hi i have problem in read/write file:

i need some help:
i have 2 file, 1- result.txt and 2-strings.txt

string is like this

I just moved to Germany two months ago and bought an 07 MDX from another military member. It has everything I could want. We just returned from a week driving through the Alps and this SUV is simply amazing. Granted, I get to drive it much faster than I could in the states,

then i need to get words from string.txt then put them on results.txt
so problem is not get words…i have words in array,
i put every word in one line of result.txt
but i have to add time of appearing(how many time this word repeated) of every word in this line,
if i result.txt if word exist i should update that line with add 1 to time of appering and if not exist i should add this word to one line of this result.txt}

result.txt is some thing like this:

yes 2
i 3
Germany 1
returned  2

i wrote below code:

 while (!feof($file_handle2)) {
    if (strpos($line2, $word ) !== FALSE){
        global $exist;
        $words = explode(" ", $line2);
        fwrite($file_handle2, $word." ".$words[2]."\n");

    //when all of the file searched
    }if ($exist==0) {
        fwrite($file_handle2,$word." ".1."."\n");

but every time i wana add one line, just first line overwriting,i wana add new line when word is not exist before in result.txt , and edit current line when word exist before in result.txt
… i know my code is wrong but how should edit or override this?

if u have any question please ASK ,

maybe you used an inappropriate mode for fopen(), refer to the manual

1 Like

Is there any reason you’re keeping the list of word counts in a stream file rather than in a database table? It just seems that it adds some complexity that might not be helpful - in particular replacing a line in the middle of a stream file causes trouble if it’s longer than the original line.

One way you might get around it would be to use two files - the current word-count file, and the new word-count file. As you work through the list of words from the current word-count file, you write the new count results into the new word-count file as you find each word. You’d also want to remove each word that appears in the current word-count file from the original string, so you know which aren’t in the file and can deal with them later.

Pseudo-code something like this:

// open current word-count file as cwc
// open new word-count file as nwc
// array of words is in $words
// read next word from cwc
// while not eof(cwc)
//   is the word from cwc in $words?
//     yes 
//       Increase the word count by the number of times it is in $words
//       Delete the word from $words wherever it occurs
//     no - do nothing
//    regardless of yes or no - write the word and the count to nwc
//   get next word from cwc
//   end of while-loop

At the end of the code, the $words array will contain all the words that aren’t already in the word-count file. So just count them up and write each one into a line in the new file.

Once you’ve finished, erase the old word-count file and rename the new one to be the proper name.

1 Like

i used file for save this values but now i think array or database is better for do this,

whats your point?

i wana add word and time of frequency and one more value, the name of doc that word was apear on this document.

like below

word frequency docnames
sam  2               doc1-doc2
germany  3       doc4
topic    5           doc8

simply: use a database. this would only be an update statement like update wordcounts set frequency = frequency + 1 where word in ... even for multiple words. and with the relations you plan, the task would be way more complex when using files. and drastically slower.


I have some data kept in files.
CSV files, XML files, JSON in txt files

They are OK when there is only a limited amount of data, they will only be used once or rarely, and they don’t need to be modified.

A drawback is the information in them needs to be parsed out and depending on what is done the amount of code needed to work with them can be non-trivial.

IMHO with anything that is going to be worked with often or might get large, a database is a much better approach. By using good queries a lot of work can be done more quickly and more efficiently by the database engine than having code carry the burden.

1 Like


im using database now

thanks for your valuable times

i have 30 megabyte text file,

i hope my database handle it,my database is mysql

When you have a million times that much your database will still be classed as tiny.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.