简体   繁体   English

在C中将空白行写入文件

[英]Write blank line to file in C

I used the write function to write some stuff into a file. 我使用了write函数将一些东西写入文件中。 now i want to insert a blank link into the file before the next write function, do anyone know how to do that? 现在我想在下一个写入功能之前在文件中插入一个空白链接,有人知道该怎么做吗?

Write(logs,log,strlen(log));

is my first write, so now is the new line going to be: 是我的第一篇文章,所以现在的新行将是:

Write(logs,'/n',strlen(log));

or should I create a new char[3] = '/n'; 还是应该创建一个新的char[3] = '/n'; ?

Assuming that you mean the POSIX write() function, something like this should work: 假设您的意思是POSIX write()函数,则应如下所示:

write(logs,"\n",1);

Note that "\\n" is a 1 character string - you may want "\\r\\n" depending on if you need a carriage return and a new line in the file. 请注意, "\\n"是一个1字符串-您可能需要"\\r\\n"具体取决于您是否需要回车符和文件中的换行符。

I know this post is pretty old but, I'am creating a log file and when I attempt to write a "\\n" it won't write it....: 我知道这篇文章已经很老了,但是,我正在创建一个日志文件,当我尝试写一个“ \\ n”时,它不会写它。...:

 write(fd,"\\n\\n==== NEW CLIENT ====\\n\\n",24); 

The output in the file is: 文件中的输出为:

 ==== NEW CLIENT ==== 

Without any blank line...any idea ? 没有任何空白行...任何想法吗? even if I put: 即使我把:

 write(fd,"\\n",1); 

nothing to do... :/ 没事做... :/

I got it...i was compiling the program in linux and checking the log file in windows, if I open the log file in linux it's ok... 我明白了...我正在linux中编译程序并在Windows中检查日志文件,如果我在linux中打开日志文件就可以了...

The first problem that you need to address is that the newline character is represented by backslash-n ( '\\n' or "\\n" ) and not slash-n. 您需要解决的第一个问题是换行符由反斜杠-n( '\\n'"\\n" )而不是斜杠-n表示。

When you write '/n' , you should be getting a compiler warning about a multi-character character constant which is a non-portable piece of C. 当您写'/n' ,您应该得到一个关于多字符字符常量的编译器警告,该常量是C的不可移植部分。

Obviously, you could create a variable to hold the newline, but there's really no need; 显然,您可以创建一个变量来保存换行符,但是实际上并不需要它。 the string literal will be be fine. 字符串文字就可以了。 If you do, the correct notation would be: 如果这样做,正确的符号将是:

const char newline[] = "\n";

You could write: 您可以这样写:

const char rather_odd[3] = { '/n' };

but...the braces are necessary, and the first character has a compiler-dependent value, and the other two characters are NUL '\\0' . 但是...大括号是必需的,并且第一个字符具有与编译器相关的值,其他两个字符为NUL '\\0' This uses more than the minimum necessary space, too; 这不仅占用了最小必要空间。 the newline variable is a 2-byte array. newline变量是一个2字节的数组。

With your non-standard Write() function, the correct syntax is probably: 使用非标准的Write()函数,正确的语法可能是:

Write(logs, "\n", 1);

This emits a single newline; 这发出一个换行符; if the previous string (the one in log ) was not itself terminated by a newline, you do not have a blank line in the file; 如果前一个字符串( log字符串)本身不是由换行符终止的,则文件中没有空行; you would have to write a second newline. 您将不得不编写第二个换行符。 You might even want to test that: 您甚至可能要测试一下:

size_t len = strlen(log);
Write(logs, log, len);
len = (log[len-1] == '\n') ? 1 : 2;
Write(logs, "\n\n", len);

Note, too, that GCC will warn you about a global variable called log (because the C standard reserves the name). 还要注意,GCC会警告您有关名为log的全局变量(因为C标准保留了该名称)。 Local variables are OK. 局部变量可以。

$ cat x.c
int x = '/n';
int log = 2;
$ gcc -c x.c
x.c:1:9: warning: multi-character character constant
x.c:2:5: warning: built-in function ‘log’ declared as non-function
$

I came across this problem recently while resurrecting some code last modified about 20 years ago. 最近,我在恢复一些大约20年前修改的代码时遇到了这个问题。

(Testing with GCC 4.5.2 on MacOS X 10.6.6.) (在MacOS X 10.6.6上使用GCC 4.5.2进行测试。)

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

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