HELP A BEGINNER!!!!!

I am rather new to programming. I am using TMT Pascal v2.11 and I need to know, how do I read and write to external files?


Comments

  • : I am rather new to programming. I am using TMT Pascal v2.11 and I need to know, how do I read and write to external files?


    IIRC, TMT pascal uses exactly the same commands Borland Pascal and the likes used to have. If I am wrong here, don't shoot me though.


    The Commands needed are Assign, Reset/Rewrite, BlockRead/BlockWrite, and Close.


    Binary files:


    var

    file1:file;

    somevariable:word;
    begin

    {first we assign the file with some filename}

    Assign(file1,"filename.abc");

    {open the file for access}

    reset(file1,1); {,1 means: 1 byte at the time}

    {we read in the first element of the file}

    blockread(file1,somevariable,2); {,2 -> 2 bytes}

    {we end by closing the file}

    close(file1);

    end.


    (hope this works ok, I am sitting here at my work, no change to actually check this or anything ;-)


    Now accessing text-files holds a different approach... once the file has been Assinged and Opened, it can be accessed with WriteLN and ReadLN, something like this:


    Text-files:



    var

    file1:text;

    teststring1,teststring2:string;
    begin

    teststring1:="this is a little test!";

    {assign file}

    Assign(file1,"bladibla.txt");

    {open file for writing}

    rewrite(file1); {note: no 2nd parameter for text-files!}

    {write the string to the file}

    writeln(file1,teststring1);

    {and close it}

    close(file1);
    {now we gonna test wether the write was succesful}

    {assign file}

    assign(file1,"bladibla.txt");

    {open for reading}

    reset(file1); {note: no 2nd parameter for text-files!}

    {read the string}

    readln(file1,teststring2);

    {close the file}

    close(file1);
    {and print the 2nd string}

    writeln("Value of the string:", teststring2);

    end.


    if I didn't make any typo's here, the program should print "this is a little test!" on the screen. Now ofcourse there is a little more to advanced file-handling than just these two little examples, so if you have any questions feel free to mail me at watcher@tuhb.org, and I'll try to answer to the best of my knowledge. If this answer was not at all what you wanted, feel free to flame me ;-)...


    Succes,

    The Watcher


    URL:http://www.tuhb.org

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories