Author Topic: How to Read text file with Slick-C Macro  (Read 6240 times)

tsunokawa

  • Junior Community Member
  • Posts: 7
  • Hero Points: 0
How to Read text file with Slick-C Macro
« on: October 22, 2019, 07:33:00 AM »
Hello
I am new to SlickEdit programming environment.
How can I read a text file with Slick-C Macro just like "fgets" function in C language?
For example, a.txt contains "1" as text.  It would be nice if I could get the "1" in the Slick-C Macro like with below.

_str readStr = xxxx("a.txt");

I searched the way, after several hours however I could not find it.
I hope what I want to do is clear. Thank you in advance.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2919
  • Hero Points: 153
Re: How to Read text file with Slick-C Macro
« Reply #1 on: October 22, 2019, 09:16:47 AM »
I'm sorry you searched so long.  There's a couple of answers to your problem.  One is to record a macro, then tweak it to suit your needs if you need to change it.  SlickEdit's macro recording actually generates Slick-C source instead of just keys.
  • Go to Macro>Record Macro
  • Open the file using the Open File tool window or dialog
  • Go to Search>Find, and find

If this doesn't do what you want, I can get you another solution, but this is very easy.

Again, sorry you looked so long.

Dennis

  • Senior Community Member
  • Posts: 3998
  • Hero Points: 521
Re: How to Read text file with Slick-C Macro
« Reply #2 on: October 22, 2019, 04:24:58 PM »
If you want the get the *entire* file in one shot, you could use a function like this:
Code: [Select]
_str get_file_contents(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   result := get_text(p_buf_size,0);
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
   return(result);
}

If you want to get all the lines in a file, and stuff them into an array, you could do something like this:
Code: [Select]
STRARRAY get_file_lines(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   _str lineContents;
   _str lineArray[];
   top();
   loop {
      get_line(lineContents);
      lineArray :+= lineContents;
      if (down()) break;
   }
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
   return lineArray;
}

If you want a function that behaves more incrementally like fgets(), you could use a function like this to get each line, until it hits the end of the file and returns null.
Code: [Select]
_str get_one_line(bool startAtFirstLine=false)
{
      if (startAtFirstLine) {
          top();
      } else if (down()) {
          return null;
      }
      get_line(auto lineContents);
      return lineContents;
}
void print_file_contents(_str filename)
{
   status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
   if (status) return null;
   lineContents := get_one_line(startAtFirstLine:true);
   for (i := 1; lineContents != null; i++) {
       say("line["i": "lineContents);
       lineContents = get_one_line();
   }
   _delete_temp_view(temp_wid);
   p_window_id=orig_wid;
}


tsunokawa

  • Junior Community Member
  • Posts: 7
  • Hero Points: 0
Re: How to Read text file with Slick-C Macro
« Reply #3 on: October 24, 2019, 01:37:44 PM »
Hi Dan,
No problem. Thank you for your replay. Maybe it is not what I wanted to do. I want to use the read value in my Macro, not using gui-open.
Thank you anyway.

Hi Dennis,
Thank you very much. It is exactly what I wanted to do. I tried your code and it worked! ("print_file_contents" needed small modification for my environment. "if (status) return null" -> "if (status) return").
Again thank you very much!

Also I found a function "_GetFileContents" which can read text file. This worked as well.

Best regards,