Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 not sure how to get Turing to write at the end of a file ie. adding a new entry to a list
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dog44




PostPosted: Tue Jan 08, 2013 8:40 pm   Post subject: not sure how to get Turing to write at the end of a file ie. adding a new entry to a list

What is it you are trying to achieve?
i am trying to get Turing to write a new student entry at the end of a file


What is the problem you are having?
i don't know how to get Turing to only write the new information at the very end or anywhere in between for that matter

Describe what you have tried to solve this problem
i have a loop system that counts the number of lines and my place markers that show when a student entree is finished but i don't know how to get it to write to that particular line (the last line basically)


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


Turing:


proc addentrie (addin : string)

    fileproc := addin
    fileproc += ".txt"
    if File.Exists (addin) then
        open : fileadd, fileproc, get
        assert fileadd > 0
        loop
            linecounter += 1
            exit when eof (fileadd)
            get : fileadd, donefinder : *
            if donefinder = "done" then
                dones += 1

            end if

        end loop
        close : fileadd
    end if
    put linecounter
    put dones
end addentrie
%=========================================================================================================================

%ADD ENTRIES PROCEDURE---------------------------------------------------------------------------------------------------
    proc loopadd (nothing :string)
        for c : 1 .. num
            exit when exitcmd = 1
            exit when exitcmd = 2
            put : file, stumarks (c).name
            count := numitems (num)
            for d : 1 .. count
                put : file, stumarks (c).item (d), " ", stumarks (c).mark (d), "/", stumarks (c).outof (d)
                if d = count then
                    put : file, stumarks (c).comments
                    put : file, "done"
                end if
            end for
        end for
        end loopadd


i am using Turing 4.1
Sponsor
Sponsor
Sponsor
sponsor
jr5000pwp




PostPosted: Tue Jan 08, 2013 9:21 pm   Post subject: Re: not sure how to get Turing to write at the end of a file ie. adding a new entry to a list

If you are attempting to write to the end of a file, all the info you will need is on this page: http://compsci.ca/holtsoft/doc/open.html
There is an example near the bottom of the page that will help with that.

As for writing to the middle, you will probably want to read the file into your custom structure (It appears you have one called stumarks), modify it how you want, then write the structure again.

For example if you have a structure of name and age and you wanted to add a structure into the file in alphabetical order you could do something like this (pseudocode/turing mixture so don't just copy and paste)

Turing:
%The data structure we want to use
type entry :
    record
        name : string
        age : int
    end record
%The array of said datastructure
var entries : flexible array 1 .. 0 of entry

%Reads all entries from the file
proc readFromFile (name : string)
    open file for reading
    %Retreive all entries in the file and put them in the array
    loop
        exit when eof
        expand entries array by 1
        entries (upper (entries)).name := readline
        entries (upper (entries)).age := readline
    end loop
    close file
end readFromFile

%Writes all entries to the file
proc writeToFile(name : string)
    open file for writing
    for i : 1 .. upper (entries)
        put entries(i).name, file
        put entries(i).age, file
    end for
    close file
end writeToFile

%Compares 2 strings and determines which comes first in the alphabet
function isGreater (name1 : string, name2 : string) : boolean
    result true if name1 is farther along in the alphabet than name2, you MIGHT be able to use > for this
end isGreater

%Adds an entry with the given properties to the array in alphabetical order
proc addEntry (name : string, age : int)
    %Remains false if the entry is at the end of the array alphabetically
    var added : boolean := false
    %Loops through all entries and checks if the entry is greater alphabetically as we assume that the array is already in alphabetical order
    for i : 1 .. upper (entries)
        if isGreater (entries (i).name, name) then
            %Expand the array, shift all entries from ui towards the end and put the current entry in at i
            expand entries array by 1
            for decreasing j : upper (entries) .. i + 1
                entries (j) := entries (j - 1)
            end for
            entries (i).name := name
            entries (i).age := age
            added := true
            exit
        end if
    end for
    %If we haven't already added the entry to the array, put it after the last entry
    if added = false then
        expand entries array by 1
        entries (upper (entries)).name := name
        entries (upper (entries)).age := age
    end if
end addEntry

readFromFile
addEntry however many times you want
writeToFile
dog44




PostPosted: Tue Jan 08, 2013 10:27 pm   Post subject: Re: not sure how to get Turing to write at the end of a file ie. adding a new entry to a list

thx for the advice i haven't tried the second half of your suggestion yet but i did try to use the first half (the link you gave me about using mod and seek) it looks like the example they gave me is either incorrect or im not doing something right cause every time i try to use it just overwrites the entire file with what i want to put in it

Turing:


   open : file, filereal, put,mod, seek
    seek : file, *
    put : file, "this should be at the end"      % the entire file just seems to get replaced with this text
    close : file


is there something im missing here ? do i have to do like put, mod in that put statement or just mod with a colon
jr5000pwp




PostPosted: Tue Jan 08, 2013 11:35 pm   Post subject: RE:not sure how to get Turing to write at the end of a file ie. adding a new entry to a list

http://compsci.ca/holtsoft/doc/seek.html has another example, however, what you put seems valid. There might be a problem somewhere else in your code, or it might be a broken feature.

Another option is to read the data in and then write it back to the file, and there are some optimizations so you don't have to use an array, but you should be able to put to the end of the file.

I do not see anything wrong with the snippet you provided, however, if you post the code (probably as a file), I can see if there's something else wrong.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: