What would be the pusedo code for a program that would write data to file?

Would it look something like this?

Declarations of type of data you want to enter.
Open the file, you want to write to.
Check if the file opened.
If file did not open try again.
Write data to the file.
Ask if satisfied.
If not keep writing.
If satisfied close file.

M I missing something?

>Open the file, you want to write to.
Aren't you forgetting to prompt the user for the filename to write to?

>If file did not open try again.
That's not necessarily the best method, because most likely if it fails the file is being used by another program, so if the file cannot be opened, usually you prompt the user for another filename (in the case of reading a file), or you just terminate the program. There's no use for wasting CPU cycles on a file that refuses to open.

>Write data to the file.
What data? When did the user enter any data?

>If not keep writing.
Keep writing? How? You've forgotten to declare a loop, which is important...

>Open the file, you want to write to.
Aren't you forgetting to prompt the user for the filename to write to?

Well thats what I men by that. What do you mean by:
Prompt the user for the filename to write to?
I had no idea there were 2 steps, in my mind you will only need one.

>If file did not open try again.
That's not necessarily the best method, because most likely if it fails the file is being used by another program, so if the file cannot be opened, usually you prompt the user for another filename (in the case of reading a file), or you just terminate the program. There's no use for wasting CPU cycles on a file that refuses to open.

I was thinking of:
File open good, if not try a different file.

>Write data to the file.
What data? When did the user enter any data?

I was going to declare the data and enter it here.

>If not keep writing.
Keep writing? How? You've forgotten to declare a loop, which is important...

I ment to say are you done? If not keep going. This is were my loop was going to come in. To check if he is done or not.

>Well thats what I men by that.
Since when did "men" come into this discussion?

>What do you mean by: Prompt the user for the filename to write to?
I mean that your program has to first ask the user for the filename, store it in a variable, and then use this variable to open the file.

>I had no idea there were 2 steps, in my mind you will only need one.
It all depends how generalistic you want to be. What I'm saying is the basic steps it would take you to write it in a language such as C/C++, however you could break it down even further into Assembly if you wanted.

>I was thinking of: File open good, if not try a different file.
Hm, and what happens if that file doesn't work? You have to cover all (or as many as are reasonable) possiblities. If the hard drive's permissions are read-only, your program would just stall or something.

>I was going to declare the data and enter it here.
Then say so. We, and your instructor can't read what you are thinking, so write it down.

>I ment to say are you done? If not keep going.
Ah, but how is the program supposed to know WHERE to keep going? Which of your previous statements is it supposed to jump to? You must explicity show that there's a loop, and that you're not just going to use a stupid GOTO to create it.

Ask the user for file name of file they wish to write data to.
Store file name in to a variable.
Open file.
If file not open issue an error message. And kill or (pause) program.
If open, ask the user to enter data.
When finished, ask user if satisfied.
If user is satisfied write to file and kill program.
If not allow user to keep going.
When done, write to file.
Close file and kill program.


I took what you sayed into consideration, how is that?
How would you do it?

The only things I would do different is that I'd explicity state that when the user enters data, it's stored in some variables. And I'd also say something like "Loop until user decides to quit" and another statement saying "End of loop".

But other than those few minor points, it's pretty much good-to-go.

I would do it like this:

1. Ask the user for file name of file they wish to write data to. 
2. Store file name in to a variable.
3. Open file. 
4. If file not open 
      issue an error message
   else
      Start a loop:
      A. Enter user data. (implies ask/accept)
      B. If finished, exit loop (5)
      C. Write data to file 
      D. Continue loop
5. Close file
commented: Very clear. Aia +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.