Dev-Pascal text file creating problem

Hello!
I was vorking in BP7 'till now but i want to try Dev-Pascal with FreePascal compiler. The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder.

Ex:
Assign(f,'something.txt');
ReWrite(f);
{...}
Close(f);

If the source file is in c:ProjectsNew then BP creates something.txt in c:ProjectsNew (c:ProjectsNewsomething.txt). In Dev-Pascal's case it's C:Dev-Passomething.txt.
So how can i set Dev-Pascal to create the text files given without path next to the soure/exe?

Comments

  • : Hello!
    : I was vorking in BP7 'till now but i want to try Dev-Pascal with FreePascal compiler. The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder.
    :
    : Ex:
    : Assign(f,'something.txt');
    : ReWrite(f);
    : {...}
    : Close(f);
    :
    : If the source file is in c:ProjectsNew then BP creates something.txt in c:ProjectsNew (c:ProjectsNewsomething.txt). In Dev-Pascal's case it's C:Dev-Passomething.txt.
    : So how can i set Dev-Pascal to create the text files given without path next to the soure/exe?
    :
    I'm not familiar with Dev-Pascal, so the functions might be different, but the trick is usable.

    After the user typed the filename, you check if it contains a path (by checking for backslashes for example). If it doesn't contain one, you add the path to the executable. In TP this can be found using the ParamStr(0) function (giving the full filename). You then need to remove the executable's filename to leave the path. In Delphi that can be done using the ExtractFilePath() function, but it isn't hard to code it yourself.
  • : : Hello!
    : : I was vorking in BP7 'till now but i want to try Dev-Pascal with FreePascal compiler. The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder.
    : :
    : : Ex:
    : : Assign(f,'something.txt');
    : : ReWrite(f);
    : : {...}
    : : Close(f);
    : :
    : : If the source file is in c:ProjectsNew then BP creates something.txt in c:ProjectsNew (c:ProjectsNewsomething.txt). In Dev-Pascal's case it's C:Dev-Passomething.txt.
    : : So how can i set Dev-Pascal to create the text files given without path next to the soure/exe?
    : :
    : I'm not familiar with Dev-Pascal, so the functions might be different, but the trick is usable.
    :
    : After the user typed the filename, you check if it contains a path (by checking for backslashes for example). If it doesn't contain one, you add the path to the executable. In TP this can be found using the ParamStr(0) function (giving the full filename). You then need to remove the executable's filename to leave the path. In Delphi that can be done using the ExtractFilePath() function, but it isn't hard to code it yourself.
    :
    To make it more simple there already is a proceure to extract the path of the exe file from the full path. It is in the dos unit and here is it's reference:
    Procedure FSplit (path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);

    And here is the example of how to use it to extract the exe path.
    [CODE]
    program test;
    uses dos;
    var path, dirstr, name, ext: string[255];
    f: text;
    begin
    path:=paramstr(0);
    FSplit(path, dirstr, name, ext);
    writeln('Program path: ', dirstr);
    assign(f, dirstr);
    rewrite(f);
    write(f, 'This is a test.');
    close(f);
    readln
    end.
    [/CODE]
    Hope it helps.

  • : : : Hello!
    : : : I was vorking in BP7 'till now but i want to try Dev-Pascal with FreePascal compiler. The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder.
    : : :
    : : : Ex:
    : : : Assign(f,'something.txt');
    : : : ReWrite(f);
    : : : {...}
    : : : Close(f);
    : : :
    : : : If the source file is in c:ProjectsNew then BP creates something.txt in c:ProjectsNew (c:ProjectsNewsomething.txt). In Dev-Pascal's case it's C:Dev-Passomething.txt.
    : : : So how can i set Dev-Pascal to create the text files given without path next to the soure/exe?
    : : :
    : : I'm not familiar with Dev-Pascal, so the functions might be different, but the trick is usable.
    : :
    : : After the user typed the filename, you check if it contains a path (by checking for backslashes for example). If it doesn't contain one, you add the path to the executable. In TP this can be found using the ParamStr(0) function (giving the full filename). You then need to remove the executable's filename to leave the path. In Delphi that can be done using the ExtractFilePath() function, but it isn't hard to code it yourself.
    : :
    : To make it more simple there already is a proceure to extract the path of the exe file from the full path. It is in the dos unit and here is it's reference:

    In FPC there is a better function (the one zibadian suggested). Instead of:

    [code]
    path:=paramstr(0);
    FSplit(path, dirstr, name, ext);
    [/code]

    You can use:

    [code]
    DirStr := ExtractFilePath(ParamStr(0));
    [/code]

    And just a note to the original message. Your statement of "The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder." is only partially correct. In both cases the text file is created in the current working directory. So if you were to run your FPC created program manually, not through the IDE, it should work the same as the BP program. But it's definitely a better solution to use ExtractFilePath(ParamStr(0)) so you can always guarantee you're looking in the right place for files.
  • : To make it more simple there already is a proceure to extract the path of the exe file from the full path. It is in the dos unit and here is it's reference:
    : Procedure FSplit (path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
    :
    : And here is the example of how to use it to extract the exe path.
    : [CODE]
    : program test;
    : uses dos;
    : var path, dirstr, name, ext: string[255];
    : f: text;
    : begin
    : path:=paramstr(0);
    : FSplit(path, dirstr, name, ext);
    : writeln('Program path: ', dirstr);
    : assign(f, dirstr);
    : rewrite(f);
    : write(f, 'This is a test.');
    : close(f);
    : readln
    : end.
    : [/CODE]
    : Hope it helps.
    It helps! Thanks! If someone is having the same problem as i am then here is the correct version of the code:
    [code]
    program test;
    uses dos;
    var path:pathstr;
    dir:dirstr;
    name:namestr;
    ext:extstr;
    f: text;
    begin
    path:=paramstr(0);
    FSplit(path, dir, name, ext);
    writeln('Program path: ', dir);
    assign(f, dir+'valami.txt');
    rewrite(f);
    write(f, 'This is a test.');
    close(f);
    readln;
    end.
    [/code]
    Beware of the Assign(f,[b]dir+[/b]'filename.extension');
  • [b][red]This message was edited by ShadowK at 2004-2-21 12:9:30[/red][/b][hr]
    : In FPC there is a better function (the one zibadian suggested). Instead of:
    :
    : [code]
    : path:=paramstr(0);
    : FSplit(path, dirstr, name, ext);
    : [/code]
    :
    : You can use:
    :
    : [code]
    : DirStr := ExtractFilePath(ParamStr(0));
    : [/code]
    :
    : And just a note to the original message. Your statement of "The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder." is only partially correct. In both cases the text file is created in the current working directory. So if you were to run your FPC created program manually, not through the IDE, it should work the same as the BP program. But it's definitely a better solution to use ExtractFilePath(ParamStr(0)) so you can always guarantee you're looking in the right place for files.
    :

    Well i think that what i wrote was right because i said "Dev-Pas creates..." and not "the compiler's bad because..." or "the exe creates..." I know that running the exe manually will work the same as the BP program. The trouble was that while coding/testing/debugging it's annoying to run it every time manually.
    Thanks for the help!
    I'll try both [b]path:=paramstr(0); FSplit(path, dirstr, name, ext);[/b] and [b]DirStr := ExtractFilePath(ParamStr(0));[/b]


  • [b][red]This message was edited by ShadowK at 2004-2-21 12:19:13[/red][/b][hr]
    : In FPC there is a better function (the one zibadian suggested). Instead of:
    :
    : [code]
    : path:=paramstr(0);
    : FSplit(path, dirstr, name, ext);
    : [/code]
    :
    : You can use:
    :
    : [code]
    : DirStr := ExtractFilePath(ParamStr(0));
    : [/code]
    :
    : And just a note to the original message. Your statement of "The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder." is only partially correct. In both cases the text file is created in the current working directory. So if you were to run your FPC created program manually, not through the IDE, it should work the same as the BP program. But it's definitely a better solution to use ExtractFilePath(ParamStr(0)) so you can always guarantee you're looking in the right place for files.
    :

    I guess there isn't another way!
    [code]
    DirStr := ExtractFilePath(ParamStr(0));
    [/code]
    Result: Error: Identifier not found EXTRACTFILEPATH

    I'll stick to bpajk's way. Thank you all for helping!


  • : [b][red]This message was edited by ShadowK at 2004-2-21 12:19:13[/red][/b][hr]
    : : In FPC there is a better function (the one zibadian suggested). Instead of:
    : :
    : : [code]
    : : path:=paramstr(0);
    : : FSplit(path, dirstr, name, ext);
    : : [/code]
    : :
    : : You can use:
    : :
    : : [code]
    : : DirStr := ExtractFilePath(ParamStr(0));
    : : [/code]
    : :
    : : And just a note to the original message. Your statement of "The problem is that BP creates the text files next to the exe when no path is specified, Dev-Pas creates it in their own folder." is only partially correct. In both cases the text file is created in the current working directory. So if you were to run your FPC created program manually, not through the IDE, it should work the same as the BP program. But it's definitely a better solution to use ExtractFilePath(ParamStr(0)) so you can always guarantee you're looking in the right place for files.
    : :
    :
    : I guess there isn't another way!
    : [code]
    : DirStr := ExtractFilePath(ParamStr(0));
    : [/code]
    : Result: Error: Identifier not found EXTRACTFILEPATH
    :
    : I'll stick to bpajk's way. Thank you all for helping!

    It's in the SysUtils unit so you have to include that. But whatever works for you.
  • : : To make it more simple there already is a proceure to extract the path of the exe file from the full path. It is in the dos unit and here is it's reference:
    : : Procedure FSplit (path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
    : :
    : : And here is the example of how to use it to extract the exe path.
    : : [CODE]
    : : program test;
    : : uses dos;
    : : var path, dirstr, name, ext: string[255];
    : : f: text;
    : : begin
    : : path:=paramstr(0);
    : : FSplit(path, dirstr, name, ext);
    : : writeln('Program path: ', dirstr);
    : : assign(f, dirstr);
    : : rewrite(f);
    : : write(f, 'This is a test.');
    : : close(f);
    : : readln
    : : end.
    : : [/CODE]
    : : Hope it helps.
    : It helps! Thanks! If someone is having the same problem as i am then here is the correct version of the code:
    : [code]
    : program test;
    : uses dos;
    : var path:pathstr;
    : dir:dirstr;
    : name:namestr;
    : ext:extstr;
    : f: text;
    : begin
    : path:=paramstr(0);
    : FSplit(path, dir, name, ext);
    : writeln('Program path: ', dir);
    : assign(f, dir+'valami.txt');
    : rewrite(f);
    : write(f, 'This is a test.');
    : close(f);
    : readln;
    : end.
    : [/code]
    : Beware of the Assign(f,[b]dir+[/b]'filename.extension');
    :
    Ups, must have missed that. The FPC didn't save the last compiled program, so I gave you the example with a bug in it, not the latest work. But I think Manning has a better solution. If it works for you, it neads only one variable, while mine needs at least three.
  • : Ups, must have missed that. The FPC didn't save the last compiled program, so I gave you the example with a bug in it, not the latest work. But I think Manning has a better solution. If it works for you, it neads only one variable, while mine needs at least three.
    :

    Ok. I wrote a unit with ChDirToScrDir() procedure. But where do i place units in Dev-Pascal or FreePascal?
  • : : Ups, must have missed that. The FPC didn't save the last compiled program, so I gave you the example with a bug in it, not the latest work. But I think Manning has a better solution. If it works for you, it neads only one variable, while mine needs at least three.
    : :
    :
    : Ok. I wrote a unit with ChDirToScrDir() procedure. But where do i place units in Dev-Pascal or FreePascal?
    :
    You can put you're compiled unit in the program directory (for use in you're program only) or if you want to use you're unit in every project put it in the units direcory in Dev pascal or Free pascal compiler.
    Example for Free pascal: "ppunitswin32
    tl" and I think just "units" for Dev Pascal. By the way you can change the current path of the program with procedure ChDir (Directory: String);
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