Results 1 to 5 of 5

Thread: Getting int from file

  1. #1
    Join Date
    Jun 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Getting int from file

    Here's a little excerpt from my code:

    Code:
    {i is already defined as: var i: integer;}
    {open is defined as: var open: boolean;}
    
    begin
    filenum := OpenFile('C:\--------------------\cbcountercount.txt', false);
    open := ReadFileInt(filenum, i);
    if (open)then
    begin
    writeln(filenum);
    writeln('Sucess in openning file!');
    writeln(inttostr(i));
    end;
    Everything seems to work except the value returned... which seems to be related to the number in the file (For example, if I put 0, it returns like 1323; if I put 100 in the file, I get like 14312514 back. They are always the same for whatever number I put in).

    Or am I using the ReadFileInt function completely wrong?

    Thanks!

  2. #2
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Yes, you are using it completely wrong. The function reads a binary file, not a text file. What you're doing is squishing a string into an integer. If you want to be able to read integers from it, you must first put them into the file by using WriteFileInt.

  3. #3
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by bullzeye95 View Post
    Yes, you are using it completely wrong. The function reads a binary file, not a text file. What you're doing is squishing a string into an integer. If you want to be able to read integers from it, you must first put them into the file by using WriteFileInt.
    Or you could combine ReadFileString with StrToInt if you really wanted to use a regular text file. You'd use it like this in your original code if you wanted to:
    SCAR Code:
    {i is already defined as: var i: integer;}
    {open is defined as: var open: boolean;}

    begin
    filenum := OpenFile('C:\--------------------\cbcountercount.txt', false);
    open := ReadFileString(filenum, j, FileSize(filenum));//note that j would have to first be declared as a string
    if (open)then
    begin
    i := StrToInt(j);
    writeln(filenum);
    writeln('Sucess in openning file!');
    writeln(inttostr(i));
    end;

  4. #4
    Join Date
    Jun 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Oh wow that is complicated =o

    Thanks so much, though. It works now!

  5. #5
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •