473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

need some help with I/O

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
7 1805
"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
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

"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

"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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.