mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Nov 2011
Posts: 52
E
eawedat Offline OP
Babel fish
OP Offline
Babel fish
E
Joined: Nov 2011
Posts: 52
hey all,
I am trying to write into a text file..
Each time I "fopen & fwrite" it writes/overrides at first line..

I need to write to the line after the last line.
Like appending...!! >>
How could I do that with fseek function ?


Suggestion but a long-way:

Code:
1)use while loop to count all lines of a text file!

2)use fseek to get down to the last line 	

/fseek -l <name> <linenumber>
/fseek -n <name>

3)fwrite -n <name> <text>



thanks.

Last edited by eawedat; 11/11/11 07:45 PM.
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
If you want to go to the very end just use the size of the file as an argument to /fseek.
Code:
fopen fp myfile.txt
fseek fp $file(myfile.txt).size
fwrite fp mydata
fclose fp


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Nov 2011
Posts: 52
E
eawedat Offline OP
Babel fish
OP Offline
Babel fish
E
Joined: Nov 2011
Posts: 52
thank you very much starbucks_mafia , that was very helpful smile

regarding my previous post! is there a built-in function that counts total lines of text file?

thanks again.

Last edited by eawedat; 11/11/11 09:41 PM.
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Yes, $lines(myfile.txt) will return the number of lines.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
If you are only writing a single line to the text file, (IE: U use .fopen, .fwrite, .fclose) in a single codeblock it would be faster to use /write. Now if u are writing to a file over and over again, fwrite would be the best option smile


I am SReject
My Stuff
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Are there benchmarks for this claim? Either method works just as fast for me:

Code:
alias benchfwrite { .fopen -o x test.txt | .fwrite -n x Hello world | .fclose x }
alias benchwrite { write test.txt Hello world }

; Commands:
; /bench 1000 benchfwrite
; /bench 1000 benchwrite
;
; Results:

Command completed in 156 ticks (0.156 avg over 1000 runs): 1000 benchfwrite
Command completed in 187 ticks (0.187 avg over 1000 runs): 1000 benchwrite
Command completed in 250 ticks (0.25 avg over 1000 runs): 1000 benchfwrite
Command completed in 203 ticks (0.203 avg over 1000 runs): 1000 benchwrite
Command completed in 203 ticks (0.203 avg over 1000 runs): 1000 benchfwrite
Command completed in 203 ticks (0.203 avg over 1000 runs): 1000 benchwrite


Perhaps you meant "simpler"? /write is certainly simpler, no denying that.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
Sorry for the terminolgy, I did mean simpler smile


But I must say, Your benchfwrite is off; you need to /fseek to the end of the file before you /fwrite to it smile

Code:
alias benchfwrite {
  .fopen bfw wfwtest.txt
  .fseek -n bfw $file(wfwtest.txt).size
  .fwrite bfw test
  .fclose bfw
}
alias benchwrite {
  write wfwtest.txt test
}



alias benchit {
  ;create the file so /fopen won't freak out
  /write wfwtest.txt

  var %x = $1, %t = $ticks
  while (%x) {
    benchfwrite
    dec %x
  }
  %t = $calc($ticks - %t)
  .remove wfwtest.txt

  ;so /write is on the same playing field as /fopen|seek|write|close
  /write wfwtest.txt

  var %y = $1, %u = $ticks
  while (%y) {
    benchwrite
    dec %y
  }
  %u = $calc($ticks - %u)
  .remove wfwtest.txt

  echo -s Results for /fwrite - Ilterations: $1 - Total Time: %t $+ ms - Avg Time: $calc(%t / $1) $+ ms
  echo -s Results for /write - Ilterations: $1 - Total Time: %u $+ ms - Avg Time: $calc(%u / $1) $+ ms

}


Code:
Results for /fwrite - Ilterations: 100000 - Total Time: 98719ms - Avg Time: 0.98719ms
Results for /write - Ilterations: 100000 - Total Time: 37235ms - Avg Time: 0.37235ms

Last edited by FroggieDaFrog; 12/11/11 04:51 AM.

I am SReject
My Stuff
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Seeking doesn't change the benchmark, here's code and a random sampling:

Code:
alias benchfwrite { .fopen x test.txt | .fseek x $file(test.txt).size | .fwrite -n x Hello world | .fclose x }
alias benchwrite { write test.txt Hello world }


; Results

Command completed in 218 ticks (0.218 avg over 1000 runs): 1000 benchwrite
Command completed in 219 ticks (0.219 avg over 1000 runs): 1000 benchfwrite
Command completed in 218 ticks (0.218 avg over 1000 runs): 1000 benchfwrite
Command completed in 234 ticks (0.234 avg over 1000 runs): 1000 benchfwrite
Command completed in 249 ticks (0.249 avg over 1000 runs): 1000 benchfwrite
Command completed in 218 ticks (0.218 avg over 1000 runs): 1000 benchwrite
Command completed in 187 ticks (0.187 avg over 1000 runs): 1000 benchwrite
Command completed in 188 ticks (0.188 avg over 1000 runs): 1000 benchwrite
Command completed in 172 ticks (0.172 avg over 1000 runs): 1000 benchwrite
Command completed in 172 ticks (0.172 avg over 1000 runs): 1000 benchwrite
Command completed in 187 ticks (0.187 avg over 1000 runs): 1000 benchwrite
Command completed in 250 ticks (0.25 avg over 1000 runs): 1000 benchfwrite
Command completed in 203 ticks (0.203 avg over 1000 runs): 1000 benchfwrite
Command completed in 203 ticks (0.203 avg over 1000 runs): 1000 benchfwrite
Command completed in 187 ticks (0.187 avg over 1000 runs): 1000 benchfwrite
Command completed in 234 ticks (0.234 avg over 1000 runs): 1000 benchfwrite


All seeking does is set an integer on an internal data structure, it would seem extremely fishy to me if the seek alone could be responsible for a difference of 100% slowdown... especially when the only difference is the overhead of the extra script parsing (mIRC is already fseeking internally when using /write, so it's not fseek() itself that is the slow bit). My guess is either you have this problem without seeking (you should take out and re-test the benchmark to confirm you have the same initial values) or your benchmark is broken.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Apr 2010
Posts: 969
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
You might want to check your /benchfwrite. if test.txt doesn't exists, /fopen throws an error "* fopen unable to open 'x'"

and I posted my bench test alias for the two commands. Do you see nd error in it?

Im not trying to argue, or undermind you, was just pointing out that a simple /write command:
- opens the specified file(or creates it)
- seeks to the end of the file
- writes the specified data
- closes the file

Which is why I mentioned your benchfwrite alias was a little sckewed smile

Last edited by FroggieDaFrog; 12/11/11 02:29 PM.

I am SReject
My Stuff
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
I tested with an already created file in both cases. The benchmark is fine, it just requires the file to exist. You can create it in the benchmark to, but it's irrelevant.

The point here is that yes, /write does everything you said, but fseek() is a very quick function-- the benchmark results shows this. Ultimately the only performance cost you should have is the cost of parsing the mIRC commands-- because the operations internally are all the same, namely:

mIRC opens the file, seeks to end, writes and closes. It does this in /write and via /fopen,/fseek,/fwrite,/fclose. It should be performing those and only those operations, no more, no less... so logically the only cost is overhead of parsing /fopen,/fseek,/fwrite,/fclose vs. just parsing /write, that is the only functional difference. I would find it difficult to believe that the parsing cost could be anything more than a few ticks.. which is why your benchmark results are surprising and look fishy.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
There's definitely something wrong with your benchmark alias although I'm not sure what.

Here's my results (using my own):

1,000 iterations:

Fwrite: 281 ms.
Write: 266 ms.

10,000 iterations:

Fwrite: 2812 ms.
Write: 2719 ms.

Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
The problem with FroggieDaFrog's benchmark is that he's using .fseek -n bfw $file(wfwtest.txt).size, so instead of seeking to the end of the file it's actually ignoring the position parameter and just performing the -n switch search, ie. seeking to the next line, which requires mIRC to read a portion of the file to find where the next line starts.

Removing the -n switch reduces run-time of the /f* command set by 20-40% depending on which system I run the benchmark on.

Of course this is largely academic; if your intention is to write a single line to the end of a text file then /write is obviously a more suitable method since it's a quick and simple one-liner.


Spelling mistakes, grammatical errors, and stupid comments are intentional.

Link Copied to Clipboard