need some help with I/O

L

Les Coover

I have struggled and struggled with this and still can not get it.

The endless loop is supposed to be that way so that
strings can be entered over and over again. When finished
CTRL + C returns to UNIX $ prompt. I know this is crude
but I am just trying to get the basics. All I want to do
is enter data into a text file, the following code compiles
and runs, creating a text file, but will not place the input 01_green
into the text file.

Thanks in advance for any help.

/* jeto.c */

#include <stdio.h>

int main(void)
{
FILE *file; /* FILE pointer */

char color[100];
/* create a file for writing */
file = fopen ("jeto.txt", "w");
printf("Enter, Example: 01_green\n\n");

while(scanf("%99s",color)== 1)
{

fprintf(file, " %50s\n", color);
}

fclose(file); /* now close the file */

return 0;
}
 
I

Irrwahn Grausewitz

Les Coover said:
I have struggled and struggled with this and still can not get it.

The endless loop is supposed to be that way so that
strings can be entered over and over again. When finished
CTRL + C returns to UNIX $ prompt.

<sigh> That's one problem with multi-posting: I just asked you a
question in acllcc++ and then found that the answer is already here
in clc.

If you kill the program with Ctrl-C you cannot be sure that all
pending data is correctly written to the output file. Two possible
solutions:

- terminate your program by passing it an EOF (usually Ctrl-D on
Unix and Ctrl-Z on DOS/Win-console respectively).

- provide a way to gracefully escape the while loop, e.g. by
checking for a special reserved value entered by the user.

If this is not acceptable to you (for whatever reasons) make at
least this addition:
while(scanf("%99s",color)== 1)
{
fprintf(file, " %50s\n", color); fflush(file);
}

to make sure the contents of the output buffer is written to file
immediately. And *PRAY* that your file system isn't left in a
corrupted state when you terminate your program "the hard way".

HTH
Regards
 
S

Sheldon Simms

I have struggled and struggled with this and still can not get it.

The endless loop is supposed to be that way so that
strings can be entered over and over again. When finished
CTRL + C returns to UNIX $ prompt. I know this is crude
but I am just trying to get the basics.

You can't expect the program to work correctly when you
use Control-C to kill it. Try typing Control-D instead,
then it should work.

Why? Because when you type Control-D, your program reads an
"end of file". This causes the scanf to return 0, the
while loop ends, the output file is closed, and your
program exits normally.
 
L

Les Coover

Irrwahn Grausewitz said:
<sigh> That's one problem with multi-posting: I just asked you a
question in acllcc++ and then found that the answer is already here
in clc.

If you kill the program with Ctrl-C you cannot be sure that all
pending data is correctly written to the output file. Two possible
solutions:

- terminate your program by passing it an EOF (usually Ctrl-D on
Unix and Ctrl-Z on DOS/Win-console respectively).

- provide a way to gracefully escape the while loop, e.g. by
checking for a special reserved value entered by the user.

If this is not acceptable to you (for whatever reasons) make at
least this addition:


to make sure the contents of the output buffer is written to file
immediately. And *PRAY* that your file system isn't left in a
corrupted state when you terminate your program "the hard way".

HTH
Regards

Irrwahn

Works on MS-DOS using control-c or control-z but not on
UNIX using control-d or control-c

I will work on a graceful way to exit the loop.

Les
 
L

Les Coover

Sheldon Simms said:
You can't expect the program to work correctly when you
use Control-C to kill it. Try typing Control-D instead,
then it should work.

Why? Because when you type Control-D, your program reads an
"end of file". This causes the scanf to return 0, the
while loop ends, the output file is closed, and your
program exits normally.
Sheldon

Yes, I will work on graceful way to exit the loop, hopefully that will solve
the problem.

Les
 
F

Floyd Davidson

Les Coover said:
I have struggled and struggled with this and still can not get it.

The endless loop is supposed to be that way so that
strings can be entered over and over again. When finished
CTRL + C returns to UNIX $ prompt. I know this is crude
but I am just trying to get the basics. All I want to do
is enter data into a text file, the following code compiles
and runs, creating a text file, but will not place the input 01_green
into the text file.

Thanks in advance for any help.

First, lets reformat your code (indents are wonderful devices)
so that it is more readable...

/* jeto.c */
#include <stdio.h>

int main(void)
{
FILE *file; /* FILE pointer */
char color[100];

/* create a file for writing */
file = fopen ("jeto.txt", "w");
printf("Enter, Example: 01_green\n\n");

while(scanf("%99s",color) == 1)
{
fprintf(file, " %50s\n", color);
}

fclose(file); /* now close the file */

return 0;
}

OK... you've opened a file, jeto.txt, and then you write a few
char's to it. But you've done that through the stdio functions,
which buffer output data. There are three modes of buffering
used by stdio, none, line buffering, and block buffering.

With no buffering data is immediately written to the output
device (stderr is an example). With line buffering the data is
written each time a line is completed as indicated by a newline
character being sent (stdout is an example). With block
buffering the data is output when the block is filled (disk
files are an example).

Hence, while you are using the fprintf() function to "write"
data, all it is doing is buffering the data. No actual write to
the disk will take place until the buffer is full or some other
mechanism is used to cause a write. For example, if you repeat
the input cycle often enough you will fill the buffer and cause
data to actually be written to the file. But instead you are
typically killing the program (using ^C) without ever flushing
the data to the file.

The fflush() function is provided to cause data to be output
from the buffer. And the setvbuf() function is provided to set
the type of buffering used.

Given that your fprintf() call always includes a newline, you
could, after opening the file, change its buffering mode to
either no buffering or to line buffering to get the desired
behavior. You could also put a fflush(file); statement
immediately after the fprintf() call.

See the man pages for setvbuf() and fflush() for specifics.
 
I

Irrwahn Grausewitz

Les Coover said:
Works on MS-DOS using control-c or control-z but not on
UNIX using control-d or control-c

I will work on a graceful way to exit the loop.

That's definitely the best way to deal with the problem. And
when you're done you can forget about the call to fflush; when
the fclose is reached after exiting the loop, the output buffer
will automatically be flushed.

Regards
 
F

Floyd Davidson

Irrwahn Grausewitz said:
That's definitely the best way to deal with the problem. And
when you're done you can forget about the call to fflush; when
the fclose is reached after exiting the loop, the output buffer
will automatically be flushed.

I'm not sure I'd agree with that. As long as it is possible for
a user to abort the program with ^C, it *will* happen.
Providing a graceful exit from the loop may greatly reduce the
frequency, but it will still happen and defensive programming
would be to provide for that event too.

Hence, yes add a graceful way to exit the loop, but either leave
the fflush or change the buffering mode to line buffering. (No
buffering would work, but it will 1) thrash the disk and 2) has
no advantage since the input is also buffered, which prevents
any pending characters on an uncompleted line from being written
anyway.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top