简体   繁体   English

C 插入换行问题(作业)

[英]C insert newline issue (homework)

I'm writing a Java bytecode program using Jasmin and Yacc, and I want to use fprintf function to write "ldc \"%s\n\"\n" into aj file, but the resulting file would always lack the newline.我正在使用 Jasmin 和 Yacc 编写 Java 字节码程序,并且我想使用 fprintf function 将“ldc \”%s\n\“\n”写入 aj 文件,但生成的文件总是缺少换行符。 For example:例如:

    fprintf (fp,"ldc \"%s \\n\"\n",$6.val.get_char);

would be represented like this in the.j file:将在 .j 文件中表示如下:

    ldc "Hello" (no newline character \n added)

("Hello" is the corresponding string obtained from $6.val.get_char) How can I fix this issue? (“Hello”是从 $6.val.get_char 获得的相应字符串) 我该如何解决这个问题? Thanks!谢谢!

Edit: I've found out the issue, so the problem is solved now.编辑:我已经发现了问题,所以问题现在解决了。 Many thanks to @Rohan Bari for the help!非常感谢@Rohan Bari 的帮助!

Edit 2: The solution turns out to be exactly the same thing I did earlier, so the following code:编辑2:解决方案与我之前所做的完全一样,所以下面的代码:

     fprintf (fp,"ldc \"%s\\n\"\n",$6.val.get_char);

is already correct, the problem is simply located elsewhere.已经是正确的,问题只是在别处。 Still, Thanks for everyone who helped!不过,感谢所有帮助过的人!

What if you do something like:如果您执行以下操作怎么办:

char text[] = "Hello";
fprintf(fp, "ldc \"%s\\n\"", text);

It outputs to the file:它输出到文件:

ldc "Hello\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM