473,666 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("%9 9s",color)== 1)
{

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

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

return 0;
}
Nov 13 '05 #1
7 1822
"Les Coover" <lc******@cox.n et.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("%9 9s",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*******@free net.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*******@free net.de> wrote in message
news:n2******** *************** *********@4ax.c om...
"Les Coover" <lc******@cox.n et.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("%9 9s",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*******@free net.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**********@y ahoo.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.n et.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("%9 9s",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.co m
Nov 13 '05 #6
"Les Coover" <lc******@cox.n et.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*******@free net.de)
Nov 13 '05 #7
Irrwahn Grausewitz <ir*******@free net.de> wrote:
"Les Coover" <lc******@cox.n et.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.co m
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6323
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, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is...
5
2180
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: Objectives The purpose of this assignment is to have you practice the design of object-oriented classes, including one or more of the following concepts
0
1835
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, all dont work "Re:" need help "Re:need help"
9
2921
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
7
3301
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
15
4597
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 communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
16
2524
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2736
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
0
3946
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
20
4257
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
8360
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.