I'm trying to write from keyboard input to a txt file. I don't understand
why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
I feel the problem lies in step 2 and 3. Can someone please explain
where the following code is wrong.
#include <stdio.h>
int main(void) {
int fd;
char file_name[80];
FILE *outstream;
printf("File Name: \n");
scanf("%79s", file_name);
if((outstream = fopen(file_name, "w")) == NULL) {
fprintf(stderr, "open %s for writing failed ", file_name);
perror("because");
return 1;
}
while((fd = getchar()) != EOF)
fputc(fd, outstream);
fclose(outstream);
return 0;
} 9 3417
On Feb 6, 3:14 am, Peter Blues <peterbl...@hotmail.comwrote:
I'm trying to write from keyboard input to a txt file. I don't understand
why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
You are a bit confused about what int and ascii is.
I feel the problem lies in step 2 and 3. Can someone please explain
where the following code is wrong.
#include <stdio.h>
int main(void) {
int fd;
'fd' as a name for an int object is a bad name, because 'int fd;' is
commonly used in UNIX programming where fd stands for file descriptor.
char file_name[80];
You could change that to FILENAME_MAX
FILE *outstream;
printf("File Name: \n");
scanf("%79s", file_name);
You read at most 79 characters but the last character of file_name is
not initialized.
Either do file_name[79] = 0; or define file_name as char file_name[80]
= {0};
You also don't check the return value of scanf but I don't think that
is importand for such small code.
(althought if this code is part of a bigger project, I suggest you do
check the return value)
>
if((outstream = fopen(file_name, "w")) == NULL) {
fprintf(stderr, "open %s for writing failed ", file_name);
perror("because");
How about this
fprintf(stderr, "open %s for writing failed because %s\n", file_name,
strerror(errno));
You need to include <string.hfor strerror().
return 1;
It is not portable to return 1 from main.
The portable values are 0, EXIT_SUCCESS and EXIT_FAILURE, again, for a
small snippet it does not matter much.
If it's a bigger project change it.
}
while((fd = getchar()) != EOF)
fputc(fd, outstream);
This seems alright to me.
>
fclose(outstream);
return 0;
}
I believe your problem lies in your confusion about keyboard being
stdin.
stdin is just an input file stream.
C does not define a 'keyboard'. If you expect the above snippet to
capture keystrokes.. then you are out of luck as far as ISO C is
concerned.
You can, however use a system specific function to capture them, which
is off-topic for this newsgroup.
try a newsgroup whose subject is relevant to your system.
Peter Blues <pe********@hotmail.comwrites:
I'm trying to write from keyboard input to a txt file. I don't understand
why the while loop below doesn't achieve this.
It does. You need to say what you think is wrong. It is likely to do
with getting both the file name and the data from the same place
(stdin) but I won't say more until you say what you think is wrong!
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
All good.
#include <stdio.h>
int main(void) {
Note that you have avoided lots of the beginner errors that are so
common so it seems almost churlish to comment, but...
int main(void) {
int fd;
Not an obvious name for an input character!
char file_name[80];
FILE *outstream;
printf("File Name: \n");
scanf("%79s", file_name);
You should check the return from scanf. You can't (safely) use the
file name if the input operation failed.
>
if((outstream = fopen(file_name, "w")) == NULL) {
fprintf(stderr, "open %s for writing failed ", file_name);
perror("because");
return 1;
Better is 'return EXIT_FAILURE;' (you need to #include <stdlib.h>).
}
while((fd = getchar()) != EOF)
fputc(fd, outstream);
fclose(outstream);
return 0;
}
--
Ben.
Peter Blues wrote:
I'm trying to write from keyboard input to a txt file. I don't
understand why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
I feel the problem lies in step 2 and 3. Can someone please explain
where the following code is wrong.
#include <stdio.h>
int main(void) {
int fd;
This is a confusing name for a variable that is supposed to store a
character value or EOF.
char file_name[80];
FILE *outstream;
printf("File Name: \n");
scanf("%79s", file_name);
scanf is a tricky function to use safely. I suggest using fgets() to
capture lines (or partial lines) and then processing as necessary.
if (fgets(file_name, FILENAME_MAX, stdin) == NULL) printf("Error.\n");
else if (strchr(file_name, '\n') == NULL) {
/* fgets() has got only a partial line. You might want to retrieve
the rest of the line, but one thing at a time.
*/
}
if((outstream = fopen(file_name, "w")) == NULL) {
fprintf(stderr, "open %s for writing failed ", file_name);
perror("because");
return 1;
One is not a strictly portable return status. Use 0 or EXIT_SUCCESS or
EXIT_FAILURE. Both the symbolic constants are in stdlib.h.
}
while((fd = getchar()) != EOF)
fputc(fd, outstream);
fputc() can fail too, returning EOF if so.
fclose(outstream);
return 0;
}
It's not immediately obvious what your problem is. In what way does your
program fail?
vipps...@gmail.com wrote:
Peter Blues <peterbl...@hotmail.comwrote:>
I'm trying to write from keyboard input to a txt file.
I don't understand why the while loop below doesn't
achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd
using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
You are a bit confused about what int and ascii is.
I feel the problem lies in step 2 and 3. Can someone
please explain where the following code is wrong.
#include <stdio.h>
int main(void) {
* * * * int fd;
'fd' as a name for an int object is a bad name,
because 'int fd;' is commonly used in UNIX programming
where fd stands for file descriptor.
* * * * char file_name[80];
You could change that to FILENAME_MAX
You mean change the magic constant 80. Also, switch to
fgets over scanf.
* * * * FILE *outstream;
* * * * printf("File Name: \n");
* * * * scanf("%79s", file_name);
You read at most 79 characters but the last character of
file_name is not initialized.
%s will null terminate the string. The problem may be that
%s will stop reading after the first whitespace character.
Either do file_name[79] = 0; or define file_name as char
file_name[80] = {0};
Neither is necessary.
You also don't check the return value of scanf but I
don't think that is importand for such small code.
I do.
(althought if this code is part of a bigger project,
I suggest you do check the return value)
Given that it's not clear what the error actually is,
checking the return value will rule out certain
candidates.
* if((outstream = fopen(file_name, "w")) == NULL) {
* fprintf(stderr, "open %s for writing failed ", file_name);
* perror("because");
How about this
fprintf(stderr, "open %s for writing failed because %s\n", file_name,
strerror(errno));
You need to include <string.hfor strerror().* * * * * * * * return 1;
It is not portable to return 1 from main.
The portable values are 0, EXIT_SUCCESS and EXIT_FAILURE,
again, for a small snippet it does not matter much.
Either it matters or it doesn't. False lazyness is preventable
and it starts with the small.
If it's a bigger project change it.
* * * * }
* * * * while((fd = getchar()) != EOF)
* * * * * * * * fputc(fd, outstream);
This seems alright to me.
Check the return value from fputc. Apart from that, it
looks ok.
* * * * fclose(outstream);
Checking the result here is waranted too.
* * * * return 0;
}
--
Peter
On Wed, 06 Feb 2008 07:13:43 +0530, santosh wrote:
>while((fd = getchar()) != EOF) fputc(fd, outstream);
fputc() can fail too, returning EOF if so.
>fclose(outstream);
return 0; }
It's not immediately obvious what your problem is. In what way does your
program fail?
An example: I open an empty filename.txt, when code is run I
enter filename.txt file. I then enter arbitrary characters from the
keyboard then exit with control c. I then return to the gedit which gives
the message filename.txt has changed do you want to reload which I do but
no characters appear in the file.
In article <47***********************@news.optusnet.com.au> ,
Peter Blues <pe********@hotmail.comwrote:
>An example: I open an empty filename.txt, when code is run I enter filename.txt file. I then enter arbitrary characters from the keyboard then exit with control c. I then return to the gedit which gives the message filename.txt has changed do you want to reload which I do but no characters appear in the file.
Control-C seldom signals end-of-file. Usually control-C signals
interruption. When you interrupt the program, by default you
would be requesting to abort the program -- in which case flushing
of the output buffers is not certain to happen. Indeed, if you
interrupt the program, transfering the entered data from the
operating system to the program is not certain to happen.
--
"All is vanity." -- Ecclesiastes
On Tue, 5 Feb 2008 17:28:46 -0800 (PST), vi******@gmail.com wrote in
comp.lang.c:
On Feb 6, 3:14 am, Peter Blues <peterbl...@hotmail.comwrote:
I'm trying to write from keyboard input to a txt file. I don't understand
why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
You are a bit confused about what int and ascii is.
I feel the problem lies in step 2 and 3. Can someone please explain
where the following code is wrong.
#include <stdio.h>
int main(void) {
int fd;
'fd' as a name for an int object is a bad name, because 'int fd;' is
commonly used in UNIX programming where fd stands for file descriptor.
char file_name[80];
You could change that to FILENAME_MAX
FILE *outstream;
printf("File Name: \n");
scanf("%79s", file_name);
You read at most 79 characters but the last character of file_name is
not initialized.
What is the source of the incorrect information you have been coming
up with lately? Where did you get the idea that scanf() will not
terminate any successful input with '\0'?
However many characters scanf() reads from the standard input stream,
up to and including 79, it will place a '0' terminator at the end.
Either do file_name[79] = 0; or define file_name as char file_name[80]
= {0};
Your second suggestion is good advice, not because it is needed if the
scanf() succeeds, because however many characters it retrieves will
have a '0' terminator added at the end. It is a good idea in case
scanf() fails to read any characters at all, due encountering an
end-of-file condition, if will call fopen() with "", instead of an
uninitialized array of characters.
Your first suggestion, file_name[79] = 0; (prefer '\0' myself), does
not protect against this problem.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Peter Blues wrote:
On Wed, 06 Feb 2008 07:13:43 +0530, santosh wrote:
>>while((fd = getchar()) != EOF) fputc(fd, outstream);
fputc() can fail too, returning EOF if so.
>>fclose(outstream);
return 0; }
It's not immediately obvious what your problem is. In what way does your program fail?
An example: I open an empty filename.txt, when code is run I
enter filename.txt file. I then enter arbitrary characters from the
keyboard then exit with control c.
In most systems CONTROL-C usually leads to the OS terminating the
program, which might result in the file not being properly written. Use
CONTROL-D under UNIX or CONTROL-Z under DOS to signal end-of-file which
is what you want to exit the read loop.
I then return to the gedit which
gives the message filename.txt has changed do you want to reload which
I do but no characters appear in the file.
Peter Blues wrote:
>
I'm trying to write from keyboard input to a txt file. I don't
understand why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using
getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
On most systems you can simply use the redirection of stdin and
stdout, as follows:
#include <stdio.h>
int main(void) {
int ch; /* note int, not char */
while (EOF != (ch = getc(stdin))} putc(ch);
return 0;
}
and you run it with:
progname outfile
followed by typing the required data. End with a ctrl-Z (for
windoze) or ctrl-D (for linux) to signal end-of-file.
Later, if you wish, you can investigate using the file open/close
statements, and get the necessary information from the command
line. Then the main parameters will no longer be void.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Wodny |
last post by:
Hello.
Is it possible to return stdin to keyboard after redirecting it? For
example after command "myprogram.exe < data.txt", when I finish reading
from file I would like to be able to use...
|
by: |
last post by:
Can I append a string to stdin? How?
How can I read from stdin a whole line?
because scanf("%s", string) reads only a word even if I use ""
thanks
|
by: gc |
last post by:
I was having trouble getting fgets to read a string from stdin, it was
reading a '\n' already in the buffer.
Someone told me to rewind stdin before calling fgets. It works, but is
it permissible?
|
by: herrcho |
last post by:
What's the difference between STDIN and Keyboard buffer ?
when i get char through scanf, i type in some characters and press
enter,
then, where do the characters go ? to STDIN or Keyboard...
|
by: Darklight |
last post by:
is this correct or incorrect i just read the post below before posting
this: In fflush(stdin), what happens to flushed data?
this program is taken from sams teach yourself c in 21 days
/*...
|
by: David Warner |
last post by:
Greetings!
I need to write some C code that will decide between either reading from
stdin or take a file name from argv and process it.
The program needs to work like all of the typical unix...
|
by: kernelxu |
last post by:
hi, everyone.
now, I'am confused on such a problem:
function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0)
can set the stadard input stream unbuffered.
however, why does my program...
|
by: srbstar |
last post by:
Can a program detect if it's stdin is
coming from keyboard input vs. being
redirected from a file?
If it's from a file, I want to fputs()
the text. But if it's from the keyboard,
I won't...
|
by: sajjanharudit |
last post by:
i need to check the stdin, repeatedly for an input form the keyboard,
with out prompting the user to press a key or without returning pressed
key on screen..
now the problem is that if i use...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |