Click here to Skip to main content
15,066,928 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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()
  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;

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.
Updated 28-Feb-11 14:11pm
HimanshuJoshi 28-Feb-11 20:17pm
Added pre blocks.

1 solution

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.

brainflex 2-Mar-11 18:32pm
SAKryukov...could you check my reply to your post about the file handles and see if I have it right?
thanks very much....James
I don't see you comment or I'm not looking to right post. I did not receive the notification.

First of all, will you formally accept this answer?
I think I explained everything.

About a file handle, do the following: 1) give me exact link to that question and tell me what to look at. 2) if I have my answer or comment, notify me so I will get a e-mail notification with the reference: 2a) if this is my Answer, "Add comment" to it, 2b) if this is not my answer, but my comment, use "Reply" icon on my comment.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900