以标准"C"格式格式化输出 [英] Formatting Output in Standard 'C'

查看:146
本文介绍了以标准"C"格式格式化输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code Warriors,

我为第一年的CS课程编写的程序有两个问题.它必须用ANSI``C''编写:
这是我的算法示意图:

Hello Code Warriors,

I have 2 problems with a program I am writing for my first year CS course. It must be written in ANSI ''C'':
Here is a sketch of my algorithm:

/*********************************************/         
  int main()
  {
       initialize();
       calculateData();
       switchPage();
       close();
  }
  void initialize()
  {
       get Input File Name;
       get Output File Name;
       open Files;
       print Cover Page();
  }
******************************************
  void calculateData()
  {
    print page column headings;
    read input data;
    process data;
    write data to output file;
  }
****************************************
  void close()
  {
    close files;
    print summary page;
  }
/*****************************************/



第一个问题:是否可以在一个功能中打开文件,在另一个功能中打印数据,然后在第三个功能中将其关闭?

第二个问题:如何拆分页面?带"\ f"?我知道如何写入文件.但是如何在打印封面,标题,数据和摘要之间移动.我需要3页:封面,格式化数据和摘要页.

任何帮助将不胜感激.
jimbob18



FIRST QUESTION: Is it possible to open files in one function, print data in another, and close them in a 3rd?

SECOND QUESTION: How do I break the pages up? With a ''\f''? I know how to write to a file. But how do I move between printing cover page, headings, data, and summary. I need 3 pages: coverpage, formatted data, and summary page.

Any help will be GREATLY appreciated.
jimbob18

推荐答案

Q1 :是的,当然.执行流和堆栈帧(基于某种调用机制)与输出流有什么关系?它们是无关的.唯一的问题可能是可支持性和编码风格.如果所有三个动作都在两个三个动作中执行,则可能是意大利面条状.不同的情况是:您打开和关闭一个功能,然后在另一个功能之间调用正确的功能.您将文件句柄或流作为第二个函数的参数传递给您.相反,这非常实用:您通过参数从具体流或文件句柄中抽象出第二个函数,以便重新使用此函数.毕竟,所有代码都应该重用,永远不要重复!

Q2 :在文本格式中,没有页面"之类的东西.如果需要更多结构,请使用HTML或XML(可能需要XSL转换).这可能是另一个问题的话题.传统上,有两种方法可以在纯文本文件中创建节.这也取决于用户界面中的文本表达方式.行之间用行尾分隔符分隔,例如"\ n","\ r或" \ r \ n";它取决于操作系统("\ r \ n"在DOS/Wind中.在一种方法中,段落由双行结尾定界.另一种(首选)方法是将单行视为段落(在UI中)该行以自动换行方式呈现),因此各段之间用单个行分隔符定界.如果使用Unicode文本,则会有特殊符号段落分隔符"(0x2029)和行分隔符"(0x2028).没有通用规则在这里,没有更高的结构元素.

如果您需要完善的文本结构,建议不要使用纯文本. XML可能是最受欢迎的选择.

—SA
Q1: Yes, of course. How are the execution flow and stack frames (something calling mechanism based on) related to the output stream? They are unrelated. The only problem can be supportability and coding style. If all three actions are performed in two three different function is may be too much spaghetti-like. Different story is this: you open and close if one function, and in between call another function which does the right; you pass you file handle or stream as a parameter of the second function. This is, in contras, very practical: you abstract our your second function out of concrete stream or file handle through a parameter, so you can re-use this function. After all, all code should be reused, never repeated!

Q2: In text format, there is no such thing as "page". If you need more structure, use HTML or XML (possibly with XSL transformation). This could be a topic of different question. Traditionally, there are two approaches to making sections in a plain text file. It also depends on the text resentation in the UI. Lines are separated with line end separators like ''\n'', ''\r or ''\r\n''; it depends on OS (''\r\n'' is in DOS/Wind. In one approach, a paragraphs are delimited by double line end. Another (preferred) approach is when a single line is considered as a paragraph (in UI the line is rendered with word wrapping), so paragraphs are delimited by a single line separators. If you use Unicode text, there are special symbols "Paragraph separator" (0x2029) and "Line separator" (0x2028). There are no common rules here and no higher structure elements.

I do not recommend using plain text if you need a well-develop text structure. XML is probably the best popular option.

—SA


这篇关于以标准"C"格式格式化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆