3
$\begingroup$

I am writing a C code for solving a big set of differential equations. I am trying to use Mathematica to export some of the long expressions to C. I would like to know how I could use Write[stream, expression] without adding a newline character. An example of what I exactly want is as follows.

t=0;    
str=OpenWrite["file path"];
   WriteString[str, "A[" ]; Write[str,t ]; WriteString[str, "] = 0;"];

I want this to be printed into the file as

A[0] = 0;

instead of

A[0
] = 0;

Also, the parameter t cannot be supplied manually. "t" is set before passing it as an argument to Write or some other similar function.If this cannot work, is there any other way of doing this ? . Thanks

$\endgroup$
3
  • 2
    $\begingroup$ WriteString[str,Tostring[0]] is one way $\endgroup$
    – george2079
    Commented Jun 23, 2016 at 22:16
  • 2
    $\begingroup$ that said, its cleaner i think to build up the whole thing and use Export, eg code = {"A[" <> ToString[0] <> "]=0;", "A[" <> ToString[1] <> "]=1;"}; Export["file.c", code, "Lines"]; $\endgroup$
    – george2079
    Commented Jun 23, 2016 at 22:23
  • $\begingroup$ George, thank you very much. Your "ToString" technique worked perfectly !....This is all I needed. $\endgroup$ Commented Jun 24, 2016 at 0:58

1 Answer 1

5
$\begingroup$

There are two differences between Write and WriteString:

  1. Write adds a newline after all expressions, WriteString does not.
  2. Write uses the FormatType of the output stream (default InputForm), WriteString uses OutputForm.

WriteString does not require its argument to be a string. Therefore, the simplest fix is to just change the second Write to WriteString.

WriteString[str, "A["]; WriteString[str, 0]; WriteString[str, "] = 0;"]

(* A[0] = 0;          -- no trailing newline *)

Alternatively, we could write out all the expressions with a single call to WriteString:

WriteString[str, "A[", 0, "] = 0;"]

(* A[0] = 0;          -- no trailing newline *)

If we want a newline after the semicolon, we can add an explicit newline:

WriteString[str, "A[", 0, "] = 0;\n"]

(* A[0] = 0;          -- with trailing newline *)

or add an extra write:

WriteString[str, "A[", 0, "] = 0;"]
Write[str]

(* A[0] = 0;          -- with trailing newline *)

If we open the file using FormatType -> OutputForm, then we can use a single Write to put the statement on a line of its own:

str = OpenWrite["zot", FormatType -> OutputForm];
Write[str, "A[", 0, "] = 0;"]
Close[str];
FilePrint["zot"]
DeleteFile["zot"]

(* A[0] = 0;          -- with trailing newline *)

Aside: Symbolic C

If you are going to be doing a lot of C code generation, you might want to look at Symbolic C. It allows high-level expression of intent and takes away all the nasty string quoting and concatenation.

Needs["SymbolicC`"]

CStatement[CAssign[CArray["A", 0], 123]] // ToCCodeString

(* A[0] = 123; *)

CFunction[int, myfunction, {{int, i}}
, CReturn[COperator[Times, {i, 10}]]
] // ToCCodeString

(* 
     int myfunction(int i)
     {
     return i * 10;
     }
*)
$\endgroup$
2
  • $\begingroup$ Thank you for the quick response. I actually forgot to mention that the numbers inside Write or the parameter t in the update post above cannot be supplied manually. $\endgroup$ Commented Jun 24, 2016 at 0:47
  • $\begingroup$ Since both Write and WriteString evaluate their arguments, it does not matter whether a value is a constant, variable or arbitrary expression. $\endgroup$
    – WReach
    Commented Jun 24, 2016 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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