473,320 Members | 1,988 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,320 software developers and data experts.

stdin (keyboard) to file

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;
}
Feb 6 '08 #1
9 3458
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.
Feb 6 '08 #2
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.
Feb 6 '08 #3
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?

Feb 6 '08 #4
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
Feb 6 '08 #5
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.
Feb 6 '08 #6
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
Feb 6 '08 #7
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
Feb 6 '08 #8
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.
Feb 6 '08 #9
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

Feb 6 '08 #10

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

Similar topics

2
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...
7
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
2
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?
23
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...
11
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 /*...
11
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...
9
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...
3
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...
30
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.