By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

need some help with I/O

P: n/a
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;
}
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Les Coover" <lc******@cox.net.spam> wrote:
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #2

P: n/a
On Tue, 18 Nov 2003 14:50:26 -0600, Les Coover wrote:
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.
Nov 13 '05 #3

P: n/a

"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:n2********************************@4ax.com...
"Les Coover" <lc******@cox.net.spam> wrote:
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
--
Irrwahn
(ir*******@freenet.de)


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
Nov 13 '05 #4

P: n/a

"Sheldon Simms" <sh**********@yahoo.com> wrote in message
news:pa****************************@yahoo.com...
On Tue, 18 Nov 2003 14:50:26 -0600, Les Coover wrote:
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.

Sheldon

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

Les
Nov 13 '05 #5

P: n/a
"Les Coover" <lc******@cox.net.spam> wrote:
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.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 13 '05 #6

P: n/a
"Les Coover" <lc******@cox.net.spam> wrote:
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
"Les Coover" <lc******@cox.net.spam> wrote:
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.


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.)

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.