Shop OBEX P1 Docs P2 Docs Learn Events
File size, file seek, type a file — Parallax Forums

File size, file seek, type a file

giannissamgiannissam Posts: 22
edited 2009-06-03 00:19 in Propeller 1
Hi all,
3 questions:

1.Can someone tell me if it is possible to read the size of a file without having to read the whole file byte by byte?
(using some of the sd objects)

2. Is it possible to seek a position in a file?
for example say a routine named seek(filename,position) whose job is to seek in a file (filename) the position requested.
for example i have the text file Hello.txt with contents "Hello people". if i run seek("hello.txt",3) the pointer goes to the 1st or 2nd "l" (depending on starting counting by 0 or 1)
i know i can do it by reading some characters (equal to position) but if i want to run this routine many times i will have to read the characters lots of times... its gonna be time consuming...confused.gif

3. I have a problem on typing a file on screen.
I read character by character and print it on screen.
it works great if the thext is sequential. If for example i copy the text from this post and paste it in a file and run the type code, i get all type of characters on screen. i guess it has something to do with changing the line. How can i correct that?

Comments

  • rokickirokicki Posts: 1,000
    edited 2009-06-01 17:59
    1. Not presently (but it's not hard to add that functionality since the size is in
    the directory metadata; just need to access the right variable).

    2. Not presently; slightly harder to add but doable.

    3. Could you elaborate? I don't fully understand the problem.
  • RaymanRayman Posts: 14,829
    edited 2009-06-01 18:45
    Getting the filesize is pretty trivial...· I've added it to my:· PSM_SdCardDriver

    just adding a couple lines to Mike Green's version of rockiki's code does it...

    file just posted here:
    http://forums.parallax.com/showthread.php?p=811555

    it's one of the files in the first attachement...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • Kit MortonKit Morton Posts: 39
    edited 2009-06-01 21:15
    Hi, Giannissam

    For seeking in a file I wrote this little piece of code for my mp3 player project. I wrote this for Mike Green's fsrwFemto version of the SD driver, but I don't think there is any reason it shouldn't work in Rokicki's original code. Just paste this method into the fsrw object. When you call it, it skips forward in the currently open file by the parameter you pass it.
    pub seek(Count) | r, t
       r := 0
       repeat while (count > 0)
          if (bufat => bufend)
             t := pfillbuf
             if (t =< 0)
                if (r > 0)
                   return r
                return t
          t := bufend - bufat
          if (t > count)
             t := count
          bufat += t
          r += t
          count -= t
       return r
    
    



    As for the file size, you should be able to make a method that reads the filesize variable. Something like this:
    pub getfilesize
      return filesize
    
    


    This should work if I correctly remember what the filesize variable stores. I haven't tested it though, so no guarantees it will actually work.

    -Kit

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PHRED, FIRST Team 847, Member
    Go -> PHRED
  • giannissamgiannissam Posts: 22
    edited 2009-06-02 23:07
    Thanx people for ur quick answers..
    First of all filesize question=silly tongue.gif

    there is allready a filesize variable.
    As kit said

    Pub fsize
    return filesize

    does the trick.

    Havent tried ur seek method yet kit. i'll post my results when ready.

    @rokicki

    What i am trying to do is:
    Lets say i have a text file
    Test.txt
    Hello my name is John and i live in Greece
    


    and a text file
    test1.txt
    Hello
    My name is John.
    I live in Greece
    


    if i run this code
    r:=sd.popen(string("test.txt"),"r")  
      repeat
        ch:=sd.pgetc
        text.out(ch)
        if ch<0
          quit    
      sd.pclose
    
    



    for the test.txt i get the contents of test.txt on my tv.
    but if i run it for test1.txt i get several characters but not necessarily the contents of test1.txt
  • rokickirokicki Posts: 1,000
    edited 2009-06-03 00:19
    Ahh, I see. What is probably happening is your text file with multiple lines has
    carriage returns but not line feeds, or something like that, and the text.out
    routines are not interpreting them the way you want.

    If you can't figure it out from that, post the full code (and include your test file)
    and we'll take a look.
Sign In or Register to comment.