Hi, simple question but maybe with an advanced answer:
How do I get Dev-Pascal to read other files?

Example
I've tried to make a variable and make it as a text for example (var test:text;) and then assign(text,'textfile.txt'); but then I want pascal to read it and open it in the console and print out the text on the screen.

This is not the only thing I wanted to test. I want to try to make other files like pictures, textfiles and all to be opened by pressing on the exe file. Like everytime i press that *exe file all the files I've written in it will be opened.

Dunno if it works, just asking, and hopefully getting ansers. :)

Recommended Answers

All 30 Replies

Er, you are using FreePascal as the compiler, right? (Not GNU Pascal?)

To open a text file, and print every line in it prefixed by the line number:

{$I-}
var
  t: textFile;
  s: string;
  i: integer;
begin
  assignFile( t, 'fooey.txt' );
  reset( t );
  if IOResult <> 0 then
    begin
    writeln( 'fooey' );
    halt( 1 )
    end;

  i := 1;
  readln( t, s );
  while not eof( t ) do
    begin
    writeln( 'line ', i, ': ', s );
    readln( t, s );
    inc( i )
    end;

  closeFile( t )
end.

If you are using GNU Pascal I think you can still use the above, but GPC tends toward the Extended Pascal syntax, which means that external file variables must be declared as bindable, and opened using different routines than reset and closeFile.

Hope this helps.

it's Blodsheet Dev Gnu-Pascal. it won't work, had to delete closefile and change string to char cause this was the error I got:

c:\documents and settings\07patrik\skrivbord\untitled1.pas:4: warning: missing string capacity - assuming 255
c:\documents and settings\07patrik\skrivbord\untitled1.pas: In main program:
c:\documents and settings\07patrik\skrivbord\untitled1.pas:21: undeclared identifier `Closefile' (first use in this routine)
c:\documents and settings\07patrik\skrivbord\untitled1.pas:21: (Each undeclared identifier is reported only once
c:\documents and settings\07patrik\skrivbord\untitled1.pas:21: for each routine it appears in.)

And when I've changed string to char (just to test so it works) and deleted closefile it popups for.. 0,5seconds.. :/ like it did before when I tested the first thing I wrote (top)

(Sorry to respond so late. Dinner called.)

But it didn't complain about "reset"? Sounds like you are using BORLAND mode.
I used an Object Pascal thing, sorry:

instead of "assignFile" say "assign"
instead of "closeFile" say "close"

And make sure you give your string a length var s: string[ 255]; If this stuff doesn't work then you are using ISO Pascal mode, and handling files is entirely different. Let me know.

no problem.. I can't get it to work.. I have made like my posted program above, then changed "assignfile" and "closefile" to "assign" and "close".. now it popups.. but goes away directly after it comes up, like if you just make a problem like "hello world" without a readln at the end.. any more help?

Sounds like the Dev IDE is too stupid to realize that you have made a console program. I refuse to use Bloodshed anything, so I don't know anything about the program, but there should be something in there to keep the console visible. Poke around the menus and see if you can find it.

The other option is just to add

write( 'Please press ENTER to quit.' ); readln

to the end of your program (just before the end. statement). The analogy to Hello World is exactly right --the same thing is happening.

Hope this helps.

Well, it still won't work. (I'm not so often on this site so It can take a few days before coming back, sorry for that, will try to update myself earlier).

It won't open the txt file. Where should the txt be putted anyway? I putted it in the same directory as the exe and with the name as entered in pascal.

Post your code and I'll take a look at it.

program Untitled;
var
t: text;
s: string[255];
i: integer;
begin
assign( t, 'untitled.txt' );
reset( t );
if IOResult <> 0 then
begin
writeln( 'untitled' );
halt( 1 )
end;

i := 1;
readln( t, s );
while not eof( t ) do
begin
writeln( 'line ', i, ': ', s );
readln( t, s );
inc( i )
end;

close( t )
end.


(don't remember the pascal code for easier reading)

It just pops up a window in the console REALLY fast, and I can read "untitled" in it. but I want to open the txt file in the "assigned application" (for the file, default program to open the file format)

I compiled and executed your program with FreePascal 2.2, Delphi 5, and Turbo Pascal 4 and it works just fine for me.

The file "untitled.txt" should be in the same directory as the exe.

It is possible that the IDE is running your program in the wrong directory. Try executing it from the command line.

Hope this helps.

I just tested it with gpc also.

There is a logic error, btw. You should have:

i := 1;
while not eof( t ) do
  begin
  readln( t, s );
  writeln( 'line ', i, ': ', s );
  inc( i )
  end;

so as not to lose the last line of the file. (Unlike C and C++, Pascal is forward-capable when finding EOF.)

Hope this helps.

how?

If it is possible to read anything from the file, eof will not return true.
Therefore, while not eof, you can read and print a line of text.

Your previous code would read the last line of text, but not print it if eof was true. Hence, the last line of text was lost (read but not printed).


Wait, was that what your "how?" was for?

yeah, but I can see the text "untitled" in my program. but it's in the "commander" and is just "blinking" then it's gone.

That means that your program couldn't find the file. Try running the program from the DOS command prompt.

And how do I do that?

Read me.

Hope this helps.

well it kinda worked.. change some things in the program and then it worked..
http://i29.tinypic.com/2wd3jus.jpg

Only repeating the first sentence.
how do I fix that?

well it kinda worked.. change some things in the program and then it worked..
http://i29.tinypic.com/2wd3jus.jpg

Only repeating the first sentence.
how do I fix that?

Don't forget to readln inside the loop.

Got it working, kinda, changed "writeln" in the loop to "write" then aadded the readln so it wouldn't be a space between each lines, but the first line repeats itself still... the other lines won't read.. How do I do that?

Got it working, kinda, changed "writeln" in the loop to "write" then aadded the readln so it wouldn't be a space between each lines, but the first line repeats itself still... the other lines won't read.. How do I do that?

What?

Post your code.

program Untitled;
uses windows;
var
t: text;
s: string[255];
i: integer;
begin
assign( t, 'untitled');
reset( t );
if IOResult <> 0 then
begin
MessageBox ( 0, 'File not found!', 'Error', Mb_ok );
halt( 1 )
end;

i := 1;
while not eof( t ) do
begin
readln( t, s );
write( 'line ', i, ': ', s );
readln;
Reset( t );
inc( i )
end;
halt ( i );
close( t )
end.

program Untitled;
uses windows;
var
t: text;
s: string[255];
i: integer;
begin
assign( t, 'untitled');
reset( t );
if IOResult <> 0 then
begin
MessageBox ( 0, 'File not found!', 'Error', Mb_ok );
halt( 1 )
end;

i := 1;
while not eof( t ) do
begin
readln( t, s );
write( 'line ', i, ': ', s );
readln;
Reset( t );
inc( i )
end;
halt ( i );
close( t )
end.

Please use code tags.

I've commented out the lines you don't need:

program Untitled;
uses windows;

var
  t: text;
  s: string[255];
  i: integer;

begin
  assign( t, 'untitled');
  reset( t );
  if IOResult <> 0 then
    begin
    MessageBox ( 0, 'File not found!', 'Error', Mb_ok );
    halt( 1 )
    end;

  i := 1;
  while not eof( t ) do
    begin
    readln( t, s );
    writeln( 'line ', i, ': ', s );  // use writeln, not write
//    readln;
//    Reset( t );
    inc( i )
    end;

//  halt ( i );
  close( t )
end.

The reason you never get to EOF is because each time through the loop you were reset ting the file back to the first line. Don't do that.

Also, don't halt your program to terminate normally...

Hope this helps.

Thank you, also, I don't really know what halt do, can you tell me what it does? Read a guide about it somewhere but i never understood it..

Anyway.. Next thing on my list of things I want to do (sorry for being such a "next thing" dude) is adding a bitmap in a pascal windows application, so if we say like, the popup I made for this program comes up, it will show a pic of a note with a red line over, and beside that text it says "file not found". How do I do this?

Thank you, also, I don't really know what halt do, can you tell me what it does? Read a guide about it somewhere but i never understood it..

Anyway.. Next thing on my list of things I want to do (sorry for being such a "next thing" dude) is adding a bitmap in a pascal windows application, so if we say like, the popup I made for this program comes up, it will show a pic of a note with a red line over, and beside that text it says "file not found". How do I do this?

Halt is abnormal program termination. In Unix and DOS, programs normally terminate with exit code zero. You can say halt( 1 ); to terminate abnormally with exit code 1. The exit code tells the program that executed your application (like Windows or the command shell) whether or not your program terminated normally. (Exit code 0 is success; anything else is error.) Except in shell scripts or other cases where anyone cares, the OS will typically ignore the return code.

For the bitmap, start a new project of the type "Windows Application" (or whatever the IDE calls it) and drop a TImage, a TButton, and a TOpenDialog on the main form. Double click the TButton and you'll be placed inside a procedure named something like "TForm1.Button1Click". Add the following:

procedure TForm1.Button1Click( Sender: TObject );
  begin
  if OpenDialog1.Execute then  // if user selects a filename
    try
      // Try to load and display it
      Image1.Picture.LoadFromFile( OpenDialog1.FileName )
    except
      // On error, complain.
      MessageDlg( "fooey!", mtWarning, [mbOK], 0 )
    end
  end;

Hope this gets you started. Make sure to find some good documentation. It makes all the difference.

That got kinda complicated. Find an easier way to make a simple "error".

MessageBox (0, 'Windows has occured an error.' , 'Error', 0 + MB_ICONHAND);

the thing that's different against the popup without a bitmap picture is that this doesn't have the "mb_ok" after error',

But Still I want to try the way you sent. But I don't really know how.. :C

Can you "talk" me through the lines that NEEDS to be there for it to work? you don't need "program" and "end." cause I already know that, I just don't know the last part before the procedur code you said.. It would be really nice if you could.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.