the program is waiting for input followed by EOF - ctrl+d on most systems. the text file is not used at all.
you should really check to see if the fopen succeded.
ie:
Code:
FILE *text = NULL;
text = fopen("input.txt", "r");
if(text == NULL)
{
printf("ERROR: could not open file\n");
exit(1);
}
Thanks! I changed some stuff around and got it to work.
I'm having trouble now, again, with writing the output to both the screen and appending the same text file... it successfully appends to it, but not the same output that was outputted to the screen (every occurrence is counted as zero):
Code:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(int argc, char **argv)
{
pid_t pID = fork();
if (pID == 0) { //child
int y[255]={0}, x=0;
FILE *text=fopen("input.txt", "r");
while ((x = fgetc(text)) != EOF)
y[x]++;
fclose(text);
for(x='0'; x<'z'; x++)
{
if(isalnum(x) )
fprintf(stdout, "The number of '%c' is %d\n", x, y[x]);
}
}
else if (pID < 0) // failed to fork
{
exit(1);
}
else // parent
{
int y[255]={0}, x=0;
FILE *text=fopen("input.txt", "a");
while ((x = fgetc(text)) != EOF)
y[x]++;
for(x='0'; x<'z'; x++)
{
if(isalnum(x) )
fprintf(text, "The number of '%c' is %d\n", x, y[x]);
}
fclose(text);
}
return 0;
}
Hi
I was trying to open a file with french name on Mac OS-X with open() and fopen() but it didn't work.Do we have any POSIX unix func. which can be used to open any file with special name.
if anybody has an idea plz help.
Thanks
Mohit (1 Reply)
hello..
i make some code with C in freebsd 5.4 and compile it in solaris
somehow i succeed compile the program.
but when i run it, i got error message "Broken Pipe"
i looked out the syntax that that caused this,
fp = fopen("file.tmp","r");
does anyone know why, and how to solve this... (3 Replies)
Dear all,
I'm a newbie for PHP and TCPDF ,I have to change the URL to PDF, so I used FPDF , But it cannot convert most of the advanced HTML tags. So explored again and found TCPDF , it can do most of the tag but I cannot found to change URL to PDF. So Does anyone can point the example... (0 Replies)
Hello!
I'm having problems with fopen() call in Linux.
I have shared library (created by myself) that implements some file operations:
int lib_func(char* file_name) {
...
fd = fopen(file_name, "r");
if(!fd) {... exit with error ...}
...
do something useful using fd
...
... (2 Replies)
what is the difference between
fopen and open
fread and read
fwrite and write
open and create
why this much of functions for the i/o when everything does the same...?
What is their major difference?
In which case, which is the best to use.
:confused:'ed Collins (2 Replies)
I always assumed the fopen/freopen is very costly, so when I needed to work with many files within on process I spent extra time to implement a list of FILE * pointers to avoid extra open/reopen but it did not produced any better results.
Here is a task at hand - there is a huge stream of data... (4 Replies)
Hello,
I have a problem here, I want to write a function called"myfopen()" instead of "fopen()"
for writing this function I must not use the <stdio.h> library,
Can you help me?
thanks a lot (2 Replies)
Discussion started by: hamed.samie
2 Replies
LEARN ABOUT V7
putc
PUTC(3S)PUTC(3S)NAME
putc, putchar, fputc, putw - put character or word on a stream
SYNOPSIS
#include <stdio.h>
int putc(c, stream)
char c;
FILE *stream;
putchar(c)
fputc(c, stream)
FILE *stream;
putw(w, stream)
FILE *stream;
DESCRIPTION
Putc appends the character c to the named output stream. It returns the character written.
Putchar(c) is defined as putc(c, stdout).
Fputc behaves like putc, but is a genuine function rather than a macro. It may be used to save on object text.
Putw appends word (i.e. int) w to the output stream. It returns the word written. Putw neither assumes nor causes special alignment in
the file.
The standard stream stdout is normally buffered if and only if the output does not refer to a terminal; this default may be changed by set-
buf(3). The standard stream stderr is by default unbuffered unconditionally, but use of freopen (see fopen(3)) will cause it to become
buffered; setbuf, again, will set the state to whatever is desired. When an output stream is unbuffered information appears on the desti-
nation file or terminal as soon as written; when it is buffered many characters are saved up and written as a block. Fflush (see
fclose(3)) may be used to force the block out early.
SEE ALSO fopen(3), fclose(3), getc(3), puts(3), printf(3), fread(3)DIAGNOSTICS
These functions return the constant EOF upon error. Since this is a good integer, ferror(3) should be used to detect putw errors.
BUGS
Because it is implemented as a macro, putc treats a stream argument with side effects improperly. In particular `putc(c, *f++);' doesn't
work sensibly.
PUTC(3S)