Announcement

Collapse
No announcement yet.

Pass dynamic file name to file_Open("filename", "mode")?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pass dynamic file name to file_Open("filename", "mode")?

    Dear All,


    Looking for a solution to pass filename as (count++) to filename argument of file_Open function.

    Problem Statement- When systems starts up, it looks for number of files available(lets say, 1.TXT, 2.TXT), two files are available. If that so, systems generates 3.TXT for writing. The next time, it starts, three files available, so system generates 4.TXT.

    My problem- I am unable to pass it to the function argument, It generates a file with no name and no extension.

    Code below-

    var myfname[12];
    var hndl_file;

    file_Mount();

    count:= file_Count("*.TXT");
    count++;

    to(myfname); print(count,".TXT");

    while(file_Exists(myfname))
    count++;
    to(myfname); print(count,".TXT");
    wend
    //Till this step, everything works.

    hndl_file:=file_Open(myfname, 'w'); // myfname argument doesnt work on this statement
    file_PutS("5005",hndl_file);
    file_Close(hndl_file);

  • #2
    Good day!

    You should use a string pointer when using both file_Exists and file_Open function.

    In your code, I don't think the file_Exists function is used properly. It might be misbehaving without you noticing. To check this, you can use file_Dir to print all the files that falls under "*.TXT". This will help you verify if file_Exists actually works this way.

    To make things easier, here is a sample code you can refer to:

    Code:
    func main()
      while(!(file_Mount()))
        putstr("Drive not mounted...");
        pause(200);
        gfx_Cls();
        pause(200);
      wend
    
      var filenamePtr, filename[12], fileHndl;
          filenamePtr := str_Ptr(filename);
    
      file_Dir("*.TXT");
      print("\n");
    
      var count, recount := 0;
          count:= file_Count("*.TXT");
    
      repeat
        to (filename); print("FILE", [DEC2Z]recount++, ".TXT");
      until (!file_Exists(filenamePtr));
      print("Next file: ", [STR]filename, "\n");
      print("Extra files found: ", count - recount + 1);
    
      fileHndl := file_Open(filenamePtr, 'w');
      file_PutS("4D Systems", fileHndl);
      file_Close(fileHndl);
    
      repeat
      forever
    endfunc
    This will create a new file every time the display is reset.

    I hope this help.

    Best Regards,
    Juniel Cruz

    Comment


    • #3
      Thanks Juniel,

      Although the file_Exist() use done by me is working perfectly, What I was missing in file_Open() argument pass logic was "filenamePtr := str_Ptr(filename);" this statement and now its working.

      Comment


      • #4
        Hello, again.

        Like I said, you are using it incorrectly. And by doing so, you may run into varying results.

        If you try and change the file_Exists line from
        Code:
        repeat
          to(filename); print("FILE", [DEC2Z]recount++, ".TXT");
        until (!file_Exists(filenamePtr));
        to:
        Code:
        repeat
          to(filename); print("FILE", [DEC2Z]recount++, ".TXT");
        until (!file_Exists(filename));
        It may result to unpredictable output. Hence, you may experience it to appear to be working but actually doesn't. Just because it seems to be working on yours doesn't really mean it is working properly. I suggest you try both methods exactly as I've shown with a fresh project.

        It's better to get ahead of the issue now than notice a problem later on.

        Best Regards,
        Juniel Cruz

        Comment

        Working...
        X