A
arnuld
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite() should
write the previously entered data to a file (except if I hit the file-size
limit)
PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not
write the data to the file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
enum { INPUT_SIZE = 5 };
FILE* fp;
void write_to_file( const char* );
int main( void )
{
char arrc[INPUT_SIZE];
memset( arrc, '\0', INPUT_SIZE );
while( fgets(arrc, INPUT_SIZE, stdin) )
{
write_to_file( arrc );
}
return EXIT_SUCCESS;
}
void write_to_file( const char* arrc )
{
int arr_size;
long fwrite_bytes;
arr_size = strlen(arrc );
++arr_size;
if( ! (fp = fopen("zzz.txt", "a")) )
{
perror("FOPEN ERROR\n");
exit( EXIT_FAILURE );
}
fwrite_bytes = fwrite( arrc, 1, arr_size, fp);
printf("fwrite_bytes = %ld\n", fwrite_bytes);
if( arr_size != fwrite_bytes )
{
perror("FWRITE ERROR");
exit( EXIT_FAILURE );
}
/*
if( fclose(fp) )
{
perror("CLOSE ERROR\n");
}
*/
}
=============== OUTPUT =====================
[arnuld@dune CLS]$ gcc -ansi -pedantic -Wall -Wextra check_FILE_IO.c
[arnuld@dune CLS]$ ./a.out
lo
fwrite_bytes = 4
[arnuld@dune CLS]$ cat zzz.txt
[arnuld@dune CLS]$
In only these 3 cases, data gets written:
1) Remove the comments from the fclose(). I mean do a proper fclose().
2) You do proper exit using Ctrl-D.
3) User enters data more than the INPUT_SIZE.
but I don't want to close the file every time I have data. I want to keep
it open till I hit the size limit. The problem with fclose() is, if the
data entered is 2 bytes on each call, then it will take 500 openings and
closings, which will be very CPU intensive I think. I want this
program to be efficient in terms of CPU, memory is not the problem
here, I have got enough of it. I need to keep the file open but in doing
so a sudden quit using Ctrl-C discards everything user entered.
Any solution to the problem ?
write the previously entered data to a file (except if I hit the file-size
limit)
PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not
write the data to the file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
enum { INPUT_SIZE = 5 };
FILE* fp;
void write_to_file( const char* );
int main( void )
{
char arrc[INPUT_SIZE];
memset( arrc, '\0', INPUT_SIZE );
while( fgets(arrc, INPUT_SIZE, stdin) )
{
write_to_file( arrc );
}
return EXIT_SUCCESS;
}
void write_to_file( const char* arrc )
{
int arr_size;
long fwrite_bytes;
arr_size = strlen(arrc );
++arr_size;
if( ! (fp = fopen("zzz.txt", "a")) )
{
perror("FOPEN ERROR\n");
exit( EXIT_FAILURE );
}
fwrite_bytes = fwrite( arrc, 1, arr_size, fp);
printf("fwrite_bytes = %ld\n", fwrite_bytes);
if( arr_size != fwrite_bytes )
{
perror("FWRITE ERROR");
exit( EXIT_FAILURE );
}
/*
if( fclose(fp) )
{
perror("CLOSE ERROR\n");
}
*/
}
=============== OUTPUT =====================
[arnuld@dune CLS]$ gcc -ansi -pedantic -Wall -Wextra check_FILE_IO.c
[arnuld@dune CLS]$ ./a.out
lo
fwrite_bytes = 4
[arnuld@dune CLS]$ cat zzz.txt
[arnuld@dune CLS]$
In only these 3 cases, data gets written:
1) Remove the comments from the fclose(). I mean do a proper fclose().
2) You do proper exit using Ctrl-D.
3) User enters data more than the INPUT_SIZE.
but I don't want to close the file every time I have data. I want to keep
it open till I hit the size limit. The problem with fclose() is, if the
data entered is 2 bytes on each call, then it will take 500 openings and
closings, which will be very CPU intensive I think. I want this
program to be efficient in terms of CPU, memory is not the problem
here, I have got enough of it. I need to keep the file open but in doing
so a sudden quit using Ctrl-C discards everything user entered.
Any solution to the problem ?