0
$\begingroup$

I wrote a function that takes two strings, an integer, and an OutputStream, it's supposed to write to. The function is:

UpdateFile[datum1_String, datum2_String, indexNum_Integer, fileDescr_OutputSteam]:= 
Module[{trackingNum},
If[indexNum < 10, trackingNum = "0" <> ToString[indexNum], 
 trackingNum = ToString[indexNum]]];
WriteLine[fileDescr, trackingNum<>", "<>datum1<>", "<>datum2]]

This doesn't appear to write anything to the text file. The weird thing is, if I just do the following "by hand" (i.e. not make a function and then call it), it does write to the text file.

fileDescr=OpenWrite[StringJoin[{"C:\\Users\\Me\\OutputFiles\\","foo.txt"}]];
indexNum=1;
If[indexNum < 10, trackingNum = "0" <> ToString[indexNum], 
 trackingNum = ToString[indexNum]]];
datum1="String";
datum2="Another String";
WriteLine[fileDescr, trackingNum<>", "<>datum1<>", "<>datum2]

To try to fix the problem I have:

  • Made sure that the parameters I pass in when I call the function have the right head
  • Used StringJoin[{trackingNum, ", " , datum1, ", " , datum2}], instead of the shortcut <>
  • Cleared the function definition, using Clear[], and redefined it
  • Quit and restarted the kernel
  • Closed and reopened Mathematica

It seems like some kind of bug or glitch to me, but I'm pretty new to Mathematica in particular, so I'm not sure if this is behavior more experienced users are familiar with. Any suggestions would be greatly appreciated! Thanks, you guys!

$\endgroup$
  • $\begingroup$ Can you give an example of how you’re calling UpdateFile? $\endgroup$ – b3m2a1 Jul 25 at 15:40
  • $\begingroup$ @b3m2a1 I am calling it like: UpdateFile["Serial","Tower",1,fileDescr]. Also, I'm not sure if it's clear, but foo.txt is created, it's just blank. $\endgroup$ – Missyinvisible Jul 25 at 16:32
  • $\begingroup$ The big question is how is the file descriptor call working. And does the function return a result? If it doesn’t you know the call isn’t being structured quite right. $\endgroup$ – b3m2a1 Jul 25 at 19:27
  • $\begingroup$ I think the call to OpenWrite, that creates the output stream/file descriptor is working fine, since it returns an output stream and I was able to write to it, just not using my UpdateFile[] function. $\endgroup$ – Missyinvisible Jul 25 at 21:04
  • $\begingroup$ I meant does calling UpdateFile return a result. My guess is you have an issue where it's not being called quite right. $\endgroup$ – b3m2a1 Jul 25 at 21:05
0
$\begingroup$

On the off chance that it would work, I swapped the order of the input parameters to:

UpdateFile[fileDescr_OutputSteam, datum1_String, datum2_String, indexNum_Integer]:= 
Module[{trackingNum},
If[indexNum < 10, trackingNum = "0" <> ToString[indexNum], 
trackingNum = ToString[indexNum]]];
WriteLine[fileDescr, trackingNum<>", "<>datum1<>", "<>datum2]]

It now works perfectly fine, but I wonder why the order would even matter.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.