简体   繁体   English

如何在文本文件中写空行?

[英]How to write a blank line to a text file?

I'm writing a program to keep an inventory of books, it reads a list of books from a text file and also writes additional books into the file. 我正在编写一个程序来保存书籍清单,它从文本文件读取书籍列表,还将其他书籍写入文件中。 I got it to add new books to the file, but I would like to have a blank line between each book entry. 我可以将新书添加到文件中,但是我想在每个书条目之间留空行。 How can I do that? 我怎样才能做到这一点? Some other info that might be useful or needed: I'm using a structure to hold the title, author, and ISBN all in character strings. 其他一些有用或需要的信息:我正在使用一种结构来将标题,作者和ISBN全部保存在字符串中。

Here's the part that collects the new data: It's put into a linked list. 这是收集新数据的部分:将其放入链接列表中。

puts("Enter first book title.");
while(fgets(input, 80, stdin) != NULL && input[0] != '\n')
{
current = (struct book *)malloc(sizeof(struct book));
if(head == NULL)
   head = current;
else
   prev->next = current;
current->next = NULL;

strcpy(current->title, input);
puts("Enter the author.");
fgets(current->author, 80, stdin);
puts("Enter the ISBN.");
fgets(current->ISBN, 80, stdin);

puts("Enter next book title (empty line to quit)");
prev = current;
}

Here's the part that writes to the file: 这是写入文件的部分:

input_file = fopen("library.txt", "w"); 
printf("Printing list to file...\n");
while(current != NULL)
{
  fprintf(input_file,"%s%s%s", current->title, current->author, current->ISBN);
  //fputs(newline, input_file);    
}

That's the relevant part. 这是相关的部分。 The commented part I tried, but it put newlines all over, messing with the data that was already nicely formatted. 我尝试过的带注释的部分,但它到处都是换行符,弄乱了已经格式化好的数据。 Here's a little example of how the text file should look (ignore the coloring): 这是文本文件外观的一个小例子(忽略颜色):

Title 1
Last, First
000000 (ISBN)

Title 2
Last, First
111111

etc.

When I add a book and the program writes it to the file, it adds it like this (left out ISBN for clarity): 当我添加一本书并且程序将其写入文件时,它会像这样添加它(为清晰起见,省略了ISBN):

...
Title 2
Last, First
First Added Book
Last, First
Second Added Title
Last, First
etc.

How can I get an empty line in between each book entry? 如何在每本书条目之间留空行?

Why not just insert a newline-character (\\n) in this line? 为什么不只在此行中插入换行符(\\ n)?

fprintf(input_file,"%s%s%s", current->title, current->author, current->ISBN);

-> - >

fprintf(input_file,"%s%s%s\n", current->title, current->author, current->ISBN);

FYI, if you add more \\n you get more empty lines. 仅供参考,如果您添加更多\\ n,则会得到更多的空行。

  1. Leaving the '\\n' in input . input保留'\\n' (see #4 also) (另请参阅#4)
    This code wants to work with input that has its terminating '\\n' removed. 这段代码要使用终止符'\\n'已删除的输入。 @Lee Daniel Crocker @李丹尼尔·克罗克(Lee Daniel Crocker)

     while(fgets(input, 80, stdin) != NULL) { size_t len = strlen(input); // Note: under select circumstances, len will be 0. if (len > 0 && input[i-1] == '\\n') input[--len] = 0; // replace `&& input[0] != '\\n'` with if (len == 0) break; 
  2. Add one or two '\\n' here. 在此处添加一两个'\\n' @midor @midor

     fprintf(input_file,"%s%s%s\\n", ... 
  3. Minor: simplify malloc() . 次要:简化malloc()

     // current = (struct book *)malloc(sizeof(struct book)); current = malloc(sizeof *current); 
  4. Check return value on subsequent fgets() . 检查后续fgets()返回值。 Need to eliminate the trailing \\n' here too. 也需要消除尾随的\\n'

  5. After malloc() consider initializing the entire structure. malloc()考虑初始化整个结构。

     current = malloc(sizeof *current); if (current == NULL) Handle_OOM(); memset(current, 0, sizeof *current); // or current = calloc(1, sizeof *current); 

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

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