•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 455,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 2760 | Replies: 30
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
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.
To open a text file, and print every line in it prefixed by the line number:
Pascal Syntax (Toggle Plain Text)
{$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.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
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)
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)
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
(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
If this stuff doesn't work then you are using ISO Pascal mode, and handling files is entirely different. Let me know.
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.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
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?
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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
to the end of your program (just before the
Hope this helps.
The other option is just to add
Pascal Syntax (Toggle Plain Text)
write( 'Please press ENTER to quit.' ); readln
end. statement). The analogy to Hello World is exactly right --the same thing is happening.Hope this helps.
Last edited by Duoas : Dec 3rd, 2007 at 3:17 pm.
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
•
•
Join Date: Nov 2007
Posts: 44
Reputation:
Rep Power: 2
Solved Threads: 0
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)
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)
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- New to Pascal and need to open project. (Pascal and Delphi)
- Could somebody tell me more dev-pascal code? (Pascal and Delphi)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: help me..
- Next Thread: Dialer in VB6 or Delphi that works on XP?



Hybrid Mode