fwrite()不会将数据写入文件 [英] fwrite() does not write data to file

查看:304
本文介绍了fwrite()不会将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通缉:即使我在fgets()中间执行Ctrl-C,fwrite()也应该将
先前输入的数据写入文件(除非我按文件大小

limit)

问题:如果我在fgets()中间执行Ctrl-C。 fwrite()没有

将数据写入文件。

#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,''\''',INPUT_SIZE);


while(fgets(arrc,INPUT_SIZE,stdin))

{

write_to_file(arrc);

}

返回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_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_FAILURE);

}


/ *

如果(fclose(fp))

{

perror(CLOSE ERROR \ n);

}

* /

}

===============输出============= ========

[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] $

仅在这3个案例中,数据被写入:


1)删除评论FCLOSE()。我的意思是做一个合适的fclose()。

2)你使用Ctrl-D正确退出。

3)用户输入的数据多于INPUT_SIZE。


但我不想每次有数据时关闭文件。我想保持

它打开直到达到大小限制。 fclose()的问题是,如果输入的

数据在每次调用时都是2个字节,那么它将需要500个空缺和

关闭,这将是非常占用CPU的我认为。我想这个

程序在CPU方面效率很高,内存不是问题

在这里,我已经足够了。我需要保持文件打开但是在执行

所以使用Ctrl-C突然退出会丢弃用户输入的所有内容。

解决问题的方法是什么?



-
www.lispmachine.wordpress .com

我的电子邮件是@上面的博客.Google Groups已被阻止。原因:过度垃圾邮件

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 ?


--
www.lispmachine.wordpress.com
my email is @ the above blog.Google Groups is Blocked. Reason: Excessive Spamming

推荐答案

gcc -ansi -pedantic -Wall -Wextra check_FILE_IO.c

[ arnuld @ dune CLS]
gcc -ansi -pedantic -Wall -Wextra check_FILE_IO.c
[arnuld@dune CLS]


./ a.out

lo

fwrite_bytes = 4


[arnuld @ dune CLS]
./a.out
lo
fwrite_bytes = 4

[arnuld@dune CLS]


cat zzz.txt

[arnuld @ dune CLS]
cat zzz.txt
[arnuld@dune CLS]


这篇关于fwrite()不会将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆