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

Username:   Password: 
 RegisterRegister   
 File reading
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
smool




PostPosted: Wed May 23, 2012 12:21 pm   Post subject: File reading

I have a file with over 5000 names, and I have to read them all and put it into an array. The problem is that the text file doesnt have the names on seperate lines, or even seperated by spaces. A sample of part of the file:

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA"

I try to use the usual get command, but it tries to get the whole line since there are no spaces of line breaks, and so I get an error "Input string to large for string variable".

Is there any way to get around this? to get each name individually?

Oh, and yes this is for project Euler, problem #22. I feel like it's acceptable to ask for help on this part since this isn't actually what the problem is about.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed May 23, 2012 12:26 pm   Post subject: RE:File reading

Turing strings are limited to 255 characters in size (I think)... definitely less than 5000 names. See get about reading in one character at a time.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Wed May 23, 2012 1:46 pm   Post subject: RE:File reading

1. Open the textfile
2. Go Edit > Replace
3. ???
4. Profit
Aange10




PostPosted: Wed May 23, 2012 5:45 pm   Post subject: RE:File reading

Try something like

Turing:

var names : flexible array 0 .. 0 of string
names (0) := ""
loop
    exit when eof(stream)
    if  upper(names) not= 0 or names (0) not= "" then
         new names, upper(names) + 1
    end if
    get : stream, names (upper(names)) : 255
end loop
Amarylis




PostPosted: Wed May 23, 2012 8:57 pm   Post subject: RE:File reading

This worked for me in small scale (tried with the 7 words you gave me and it worked out fine), Idk how well it'll do for 5000 names

Turing:

var bigName : string
var name : flexible array 0 .. -1 of string
var st, endSt : int
var filestream : int
st := 1

open : filestream, "file.txt", get

loop
    exit when eof (filestream)
    get : filestream, bigName : 255
    loop
        exit when st >= length (bigName)
        endSt := st + index (bigName (st + 1 .. *), '"') - 1
        if bigName (st + 1 .. endSt) ~= "," then
            new name, upper (name) + 1
            name (upper (name)) := bigName (st + 1 .. endSt)
            put name (upper (name))
        end if
        st := endSt + index (bigName (endSt + 1 .. *), '"')
    end loop
end loop
smool




PostPosted: Wed May 23, 2012 10:17 pm   Post subject: RE:File reading

Okay thanks everyone, I managed to solve it Very Happy
Raknarg




PostPosted: Thu May 24, 2012 8:29 am   Post subject: RE:File reading

"This is impossible!"
"Have you tried replacing?"
"Oh..."
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  [ 7 Posts ]
Jump to:   


Style:  
Search: