写入文件 [英] Write to file

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

问题描述

您好,


我正在尝试将缓冲区内容输出到文件中。我要么获得访问

违规错误,要么在文件中疯狂查看输出,具体取决于

我用来写文件的方法。任何人都可以帮助新手吗?

#include< stdio.h>

#include< ctype.h>

#include< ; string.h>


#define显示117 / *显示行长* /

#define PAGE_LENGTH 20 / *每页行数* /


int main(int argc,char * argv [])

{

FILE * pfile;

FILE * outfile;

char * p;


unsigned char buffer [DISPLAY / 4 - 1]; / *文件输入缓冲区* /

int count = 0; / *缓冲区中的字符数* /

int lines = 0; / *显示的行数* /

int i = 0; / *循环计数器* /


char string [100];

pfile = fopen(" my.txt"," r"); < (/!feof(pfile))/ *继续直到文件结尾* /

{

if(count< sizeof buffer)/ *如果缓冲区未满* /

/ *读取一个字符* /

buffer [count ++] =(unsigned char)fgetc(pfile);

其他

{

/ *现在显示缓冲区内容为字符* /

for(count = 0; count< sizeof buffer; count ++)

printf("%c",isprint(buffer [count])?buffer [count]:''。'');

printf(" \ n"); / *结束行* /

count = 0; / *重置计数* /


if(!(++ lines%PAGE_LENGTH))/ *页面结束? * /

if(getchar()==''E'')/ *等待输入* /

返回0; / * E按* /

}

}


/ *将最后一行显示为字符* /

for(i = 0; i< count; i ++)

{

printf("%c",isprint(buffer [i])?buffer [i]:''。'');

}


printf(" \ n");

FCLOSE(PFILE); / *关闭文件* /


//重新打开文件并写出缓冲内容

outfile = fopen(" myOutput.txt", w);


如果(outfile == NULL)

{

返回0;

}


//导致访问冲突错误

for(count = 0; count< sizeof buffer; count ++)

{

fputc(outfile,isprint(buffer [count])?buffer [count]:''。'');

}


//导致文件中的疯狂输出

/ * for(count = 0; count< sizeof buffer; count ++)

{

(int)fwrite(buffer,1,isprint(buffer [count]),outfile);

} * /


fclose( outfile);

getchar();

返回0;

}

Hello,

I''m trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone help out a newbie?
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */

int main(int argc, char *argv[])
{
FILE *pfile;
FILE *outfile;
char *p;

unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */

char string [100];
pfile = fopen ("my.txt" , "r");

while(!feof(pfile)) /* Continue until end of file */
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:''.'');
printf("\n"); /* End the line */
count = 0; /* Reset count */

if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()==''E'') /* Wait for Enter */
return 0; /* E pressed */
}
}

/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:''.'');
}

printf("\n");
fclose(pfile); /* Close the file */

//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");

if(outfile==NULL)
{
return 0;
}

//causes an access violation error
for(count = 0; count < sizeof buffer; count++)
{
fputc(outfile, isprint(buffer[count]) ? buffer[count]:''.'');
}

//causes crazy output in file
/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/

fclose(outfile);
getchar();
return 0;
}

推荐答案

Bill< bi ********* @ allstate.comwrites:
Bill <bi*********@allstate.comwrites:

您好,


我正在尝试输出缓冲区内容到文件中。我要么获得访问

违规错误,要么在文件中疯狂查看输出,具体取决于

我用来写文件的方法。任何人都可以帮助新手吗?
Hello,

I''m trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone help out a newbie?



当然......

Sure...


#include< stdio.h>

#include< ctype.h>

#include< string.h>


#define DISPLAY 117 / *显示行长度* /

#define PAGE_LENGTH 20 / *每页行数* /


int main(int argc,char * argv [])

{

FILE * pfile;

FILE * outfile;

char * p;


unsigned char buffer [DISPLAY / 4 - 1]; / *文件输入缓冲区* /

int count = 0; / *缓冲区中的字符数* /

int lines = 0; / *显示的行数* /

int i = 0; / *循环计数器* /


char string [100];

pfile = fopen(" my.txt"," r");
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */

int main(int argc, char *argv[])
{
FILE *pfile;
FILE *outfile;
char *p;

unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */

char string [100];
pfile = fopen ("my.txt" , "r");



您应该检查它是否有效。我知道你知道这个!

You should check it worked. I know you know this!


while(!feof(pfile))/ *继续直到文件结尾* /
while(!feof(pfile)) /* Continue until end of file */



这会给你带来一些惊喜,也不会做你想的。你需要检查读取操作的回报(在你的情况下)fgetc:


int c;

... 。(b = fgetc(pfile))!= EOF){

...

*输入之后仅返回非零值*由于和文件结束条件,
操作失败。它应该是

仅用于告诉*为什么*事情出错了。

This will cause you some surprises and won''t do what you think. You
should check the return from the read operation (in you case) fgetc:

int c;
....
while ((c = fgetc(pfile)) != EOF) {
...

The feof function only returns a non-zero value *after* an input
operation has failed due to and end-of-file condition. It should be
used only to tell *why* things have gone wrong.


{

if( count< sizeof buffer)/ *如果缓冲区未满* /

/ *读取一个字符* /

buffer [count ++] =(unsigned char)fgetc( pfile);

其他

{

/ *现在显示缓冲区内容为字符* /

for(count = 0; count< sizeof buffer; count ++)

printf("%c",isprint(buffer [count])?buffer [count]:''。'');

printf(" \ n"); / *结束行* /

count = 0; / *重置计数* /


if(!(++ lines%PAGE_LENGTH))/ *页面结束? * /

if(getchar()==''E'')/ *等待输入* /

返回0; / * E按* /
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:''.'');
printf("\n"); /* End the line */
count = 0; /* Reset count */

if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()==''E'') /* Wait for Enter */
return 0; /* E pressed */



getchar调用通常会对缓冲输入进行处理,所以

不是一个好方法;暂停"你的计划。如果我想这样做(并且由于某种原因我不会这么多天来)我会写一个函数来阅读

整行输入和决定基于此做什么。

There getchar calls will normally be working on a buffered input so it
is not a good way to "pause" your program. If I wanted to do this (and
for some reason I don''t these days) I''d write a function to read a
whole line of input and decide what to do based on that.


}

}


/ *显示最后行为字符* /

for(i = 0; i< count; i ++)

{

printf("%c" ,isprint(buffer [i])?buffer [i]:''。'');

}


printf(" \ n" );

fclose(pfile); / *关闭文件* /
}
}

/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:''.'');
}

printf("\n");
fclose(pfile); /* Close the file */



您应该检查这是否也有效。

You should check if this worked also.


// re -open文件并写出缓冲区内容

outfile = fopen(" myOutput.txt"," w");


if(outfile = = NULL)

{

返回0;

}


//导致访问冲突错误

for(count = 0; count< sizeof buffer; count ++)

{

fputc(outfile,isprint(buffer [count] )?buffer [count]:''。'');
//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");

if(outfile==NULL)
{
return 0;
}

//causes an access violation error
for(count = 0; count < sizeof buffer; count++)
{
fputc(outfile, isprint(buffer[count]) ? buffer[count]:''.'');



您应该检查这是否有效。习惯检查

所有要检查的东西!

You should check that this worked. Get in the habit of checking
everything that there is to check!


}


//导致文件中的疯狂输出

/ * for(count = 0; count< sizeof buffer; count ++)

{

(int )fwrite(buffer,1,isprint(buffer [count]),outfile);

} * /
}

//causes crazy output in file
/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/



这将反复尝试将缓冲区中的第一个字符打印为

多次,因为缓冲区中有可打印的字符!另外,通过

这样使用它(有时候要求不写数据)你犯了错误

检查更难。坚持使用fputc方法。没事。对于

您可能想要的记录:


fwrite(& buffer [count],1,isprint(buffer [count]),outfile);


如果要打印整个缓冲区。首先处理它:


for(count = 0; count< sizeof buffer; count ++)

if(!isprint(buffer [count]))

buffer [count] =''。'';


if(fwrite(buffer,1,count,outfile)!= count)

/ *报告写入错误... * /

This will repeatedly try to print the first character in buffer as
many times as there are printable characters in the buffer!. Also, by
using it this way (asking to write no data sometimes) you make error
checking much harder. Stick with the fputc method. It is fine. For
the record you probably intended:

fwrite(&buffer[count], 1, isprint(buffer[count]), outfile);

If you want to print a whole buffer. Process it first:

for (count = 0; count < sizeof buffer; count++)
if (!isprint(buffer[count]))
buffer[count] = ''.'';

if (fwrite(buffer, 1, count, outfile) != count)
/* report a write error... */


>

fclose(outfile);

getchar();

返回0;

}
>
fclose(outfile);
getchar();
return 0;
}



-

Ben。

--
Ben.


6月13日上午6:09,Ben Bacarisse< ben.use ... @ bsb.me.ukwrote:
On Jun 13, 6:09 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:

Bill< bill.war ... @ allstate.comwrites:
Bill <bill.war...@allstate.comwrites:

你好,
Hello,


我正在尝试将缓冲区内容输出到文件。我要么获得访问

违规错误,要么在文件中疯狂查看输出,具体取决于

我用来写文件的方法。任何人都可以帮助新手吗?
I''m trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone help out a newbie?



当然......



Sure...



#include< stdio。 h>

#include< ctype.h>

#include< string.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>


#define DISPLAY 117 / *显示行长* /

#define PAGE_LENGTH 20 / *每页行数* /
#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */


int main(int argc,char * argv [])

{

FILE * pfile;

FILE * outfile;

char * p;
int main(int argc, char *argv[])
{
FILE *pfile;
FILE *outfile;
char *p;


unsigned char buffer [DISPLAY / 4 - 1]; / *文件输入缓冲区* /

int count = 0; / *缓冲区中的字符数* /

int lines = 0; / *显示的行数* /

int i = 0; / *循环计数器* /
unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */


char string [100];

pfile = fopen(" my.txt" ,r);
char string [100];
pfile = fopen ("my.txt" , "r");



您应该检查它是否有效。我知道你知道这个!


You should check it worked. I know you know this!


while(!feof(pfile))/ *继续直到文件结尾* /
while(!feof(pfile)) /* Continue until end of file */



这会给你带来一些惊喜,不会做你想的。你需要检查读取操作的回报(在你的情况下)fgetc:


int c;

... 。(b = fgetc(pfile))!= EOF){

...

*输入之后仅返回非零值*由于和文件结束条件,
操作失败。它应该是

仅用于告诉*为什么*事情出错了。


This will cause you some surprises and won''t do what you think. You
should check the return from the read operation (in you case) fgetc:

int c;
....
while ((c = fgetc(pfile)) != EOF) {
...

The feof function only returns a non-zero value *after* an input
operation has failed due to and end-of-file condition. It should be
used only to tell *why* things have gone wrong.


{

if( count< sizeof buffer)/ *如果缓冲区未满* /

/ *读取一个字符* /

buffer [count ++] =(unsigned char)fgetc( pfile);

其他

{

/ *现在显示缓冲区内容为字符* /

for(count = 0; count< sizeof buffer; count ++)

printf("%c",isprint(buffer [count])?buffer [count]:''。'');

printf(" \ n"); / *结束行* /

count = 0; / *重置计数* /
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:''.'');
printf("\n"); /* End the line */
count = 0; /* Reset count */


if(!(++ lines%PAGE_LENGTH))/ *页面结束? * /

if(getchar()==''E'')/ *等待输入* /

返回0; / * E按* /
if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()==''E'') /* Wait for Enter */
return 0; /* E pressed */



getchar调用通常会对缓冲输入进行处理,所以

不是一个好方法;暂停"你的计划。如果我想这样做(并且由于某种原因我不会这么多天来)我会写一个函数来阅读

整行输入和决定基于此做什么。


There getchar calls will normally be working on a buffered input so it
is not a good way to "pause" your program. If I wanted to do this (and
for some reason I don''t these days) I''d write a function to read a
whole line of input and decide what to do based on that.


}

}
}
}


/ *将最后一行显示为字符* /

for(i = 0; i< count; i ++)

{

printf("%c",isprint(buffer [i])?buffer [i]:''。'');

}
/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:''.'');
}


printf(" \ n");

fclose(pfile); / *关闭文件* /
printf("\n");
fclose(pfile); /* Close the file */



您应该检查这是否也有效。


You should check if this worked also.


// re - 打开文件并写出缓冲内容

outfile = fopen(" myOutput.txt"," w");
//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");


if(outfile == NULL)

{

return 0;

}
if(outfile==NULL)
{
return 0;
}


//导致访问冲突错误

for(count = 0; count< sizeof buffer; count ++)

{

fputc(outfile,isprint(buffer [count])?buffer [count]:''。'');
//causes an access violation error
for(count = 0; count < sizeof buffer; count++)
{
fputc(outfile, isprint(buffer[count]) ? buffer[count]:''.'');



您应该检查这是否有效。习惯检查

所有要检查的东西!


You should check that this worked. Get in the habit of checking
everything that there is to check!


}
}


//导致文件中的疯狂输出

/ * for(count = 0; count< sizeof buffer; count ++)

{

(int)fwrite(buffer,1,isprint(buffer [count]),outfile);

} * /
//causes crazy output in file
/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/



这将反复尝试将缓冲区中的第一个字符打印为

多次,因为缓冲区中有可打印的字符!另外,通过

这样使用它(有时候要求不写数据)你犯了错误

检查更难。坚持使用fputc方法。没事。对于

您可能想要的记录:


fwrite(& buffer [count],1,isprint(buffer [count]),outfile);


如果要打印整个缓冲区。首先处理它:


for(count = 0; count< sizeof buffer; count ++)

if(!isprint(buffer [count]))

buffer [count] =''。'';


if(fwrite(buffer,1,count,outfile)!= count)

/ *报告写错误... * /


This will repeatedly try to print the first character in buffer as
many times as there are printable characters in the buffer!. Also, by
using it this way (asking to write no data sometimes) you make error
checking much harder. Stick with the fputc method. It is fine. For
the record you probably intended:

fwrite(&buffer[count], 1, isprint(buffer[count]), outfile);

If you want to print a whole buffer. Process it first:

for (count = 0; count < sizeof buffer; count++)
if (!isprint(buffer[count]))
buffer[count] = ''.'';

if (fwrite(buffer, 1, count, outfile) != count)
/* report a write error... */


fclose(outfile);

getchar();

返回0;

}
fclose(outfile);
getchar();
return 0;
}



-

Ben.-隐藏引用的文字 -


- 显示引用的文字 -


--
Ben.- Hide quoted text -

- Show quoted text -



非常感谢。我正在取得进展。现在没有错误,但文件中的输出结果很糟糕。代码:

#include< stdio.h>

#include< ctype.h>

#include< string.h> ;


#define显示117 / *显示行长* /

#define PAGE_LENGTH 20 / *每页行数* /


int main(int argc,char * argv [])

{

FILE * pfile; / *文件指针* /

FILE * outfile;

char * p;

int c;


unsigned char buffer [DISPLAY / 4 - 1]; / *文件输入缓冲区* /

int count = 0; / *缓冲区中的字符数* /

int lines = 0; / *显示的行数* /

int i = 0; / *循环计数器* /


char string [100];

pfile = fopen(" my.txt"," r");

while((c = fgetc(pfile))!= EOF)/ *继续直到结束

档* /

{

if(count< sizeof buffer)/ *如果缓冲区未满* /

/ *读取一个字符* /

buffer [count ++] =(unsigned char)fgetc(pfile);

else

{

/ *现在显示缓冲区内容为字符* /

for(count = 0; count< sizeof buffer; count ++)

printf("%c",isprint(buffer [count])?buffer [count]:''。 '');

printf(" \ n"); / *结束行* /

count = 0; / *重置计数* /


if(!(++ lines%PAGE_LENGTH))/ *页面结束? * /

if(getchar()==''E'')/ *等待输入* /

返回0; / * E按* /

}

}


/ *将最后一行显示为字符* /

for(i = 0; i< count; i ++)

{

printf("%c",isprint(buffer [i])?buffer [i]:''。'');

}


printf(" \ n");

FCLOSE(PFILE); / *关闭文件* /


//重新打开文件并写出缓冲内容

outfile = fopen(" myOutput.txt", w);


如果(outfile == NULL)

{

返回0;

}

for(count = 0; count< sizeof buffer; count ++)

{

if(!isprint(buffer [计数]))

{

buffer [count] =''。'';

}

else

{

fputc(缓冲[计数],outfile);

}

// fputc(outfile, isprint(buffer [count])?buffer [count]:''。'');

}


/ * for(count = 0; count < sizeof buffer; count ++)

{

(int)fwrite(buffer,1,isprint(buffer [count]),outfile);

} * /


fclose(outfile);

getchar();

返回0;

}


输入文件内容:

Fred Flinstone 123-456-7890

Barney Rubble 123-789-4561


输出文件:

B12y7p456r

Thanks alot. I''m making progress. No errors now, but the output in
the file is screwy. The code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */

int main(int argc, char *argv[])
{
FILE *pfile; /* File pointer */
FILE *outfile;
char *p;
int c;

unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */

char string [100];
pfile = fopen ("my.txt" , "r");
while((c = fgetc(pfile)) != EOF) /* Continue until end of
file */
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:''.'');
printf("\n"); /* End the line */
count = 0; /* Reset count */

if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()==''E'') /* Wait for Enter */
return 0; /* E pressed */
}
}

/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:''.'');
}

printf("\n");
fclose(pfile); /* Close the file */

//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");

if(outfile==NULL)
{
return 0;
}
for(count = 0; count < sizeof buffer; count++)
{
if(!isprint(buffer[count]))
{
buffer[count] = ''.'';
}
else
{
fputc(buffer[count],outfile);
}
//fputc(outfile, isprint(buffer[count]) ? buffer[count]:''.'');
}

/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/

fclose(outfile);
getchar();
return 0;
}

Input file contents:
Fred Flinstone 123-456-7890
Barney Rubble 123-789-4561

Output file:
B12y7p456r


Bill< bi ********* @ allstate.comwrites:
Bill <bi*********@allstate.comwrites:

非常感谢。我正在取得进展。现在没有错误,但文件中的输出结果很糟糕。代码:
Thanks alot. I''m making progress. No errors now, but the output in
the file is screwy. The code:



请回复你的回复(并且总是修剪签名行)。

Please trip your replies (and always trim signature lines).


#include< ; stdio.h>

#include< ctype.h>

#include< string.h>


#定义DISPLAY 117 / *显示行长* /

#define PAGE_LENGTH 20 / *每页行* /


int main(int argc,char * argv [])

{

FILE * pfile; / *文件指针* /

FILE * outfile;

char * p;

int c;


unsigned char buffer [DISPLAY / 4 - 1]; / *文件输入缓冲区* /

int count = 0; / *缓冲区中的字符数* /

int lines = 0; / *显示的行数* /

int i = 0; / *循环计数器* /


char string [100];

pfile = fopen(" my.txt"," r");


while((c = fgetc(pfile))!= EOF)/ *继续直到结束

档* /

{

if(count< sizeof buffer)/ *如果缓冲区未满* /

/ *读取一个字符* /

buffer [count ++] =(unsigned char)fgetc(pfile);
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define DISPLAY 117 /* Length of display line */
#define PAGE_LENGTH 20 /* Lines per page */

int main(int argc, char *argv[])
{
FILE *pfile; /* File pointer */
FILE *outfile;
char *p;
int c;

unsigned char buffer[DISPLAY/4 - 1]; /* File input buffer */
int count = 0; /* Count of characters in buffer */
int lines = 0; /* Number of lines displayed */
int i = 0; /* Loop counter */

char string [100];
pfile = fopen ("my.txt" , "r");
while((c = fgetc(pfile)) != EOF) /* Continue until end of
file */
{
if(count < sizeof buffer) /* If the buffer is not full */
/* Read a character */
buffer[count++] = (unsigned char)fgetc(pfile);



我打算你在这里使用c而不是读另一个角色!

I intended you you use c here not read another character!


else

{

/ *现在显示缓冲区内容为字符* /

for(count = 0; count< sizeof buffer; count ++)

printf("%c",isprint(buffer [count])?buffer [count]:''。'');

printf(" \ n"); / *结束行* /

count = 0; / *重置计数* /
else
{
/* Now display buffer contents as characters */
for(count = 0; count < sizeof buffer; count++)
printf("%c", isprint(buffer[count]) ? buffer[count]:''.'');
printf("\n"); /* End the line */
count = 0; /* Reset count */



您的输入足够长以触发此条件。你重用了缓冲区。

Your input is long enough to trigger this condition. You reuse the buffer.


if(!(++ lines%PAGE_LENGTH))/ *页面结束? * /

if(getchar()==''E'')/ *等待输入* /

返回0; / * E按* /

}

}


/ *将最后一行显示为字符* /

for(i = 0; i< count; i ++)

{

printf("%c",isprint(buffer [i])?buffer [i]:''。'');

}


printf(" \ n");

FCLOSE(PFILE); / *关闭文件* /


//重新打开文件并写出缓冲内容

outfile = fopen(" myOutput.txt", w);


如果(outfile == NULL)

{

返回0;

}


for(count = 0; count< sizeof buffer; count ++)

{

if(! isprint(buffer [count]))

{

buffer [count] =''。'';

}

其他

{

fputc(缓冲[计数],outfile);

}
if(!(++lines%PAGE_LENGTH)) /* End of page? */
if(getchar()==''E'') /* Wait for Enter */
return 0; /* E pressed */
}
}

/* Display last line as characters */
for(i = 0; i < count; i++)
{
printf("%c",isprint(buffer[i]) ? buffer[i]:''.'');
}

printf("\n");
fclose(pfile); /* Close the file */

//re-open file and write out the buffer contents
outfile = fopen ("myOutput.txt" , "w");

if(outfile==NULL)
{
return 0;
}
for(count = 0; count < sizeof buffer; count++)
{
if(!isprint(buffer[count]))
{
buffer[count] = ''.'';
}
else
{
fputc(buffer[count],outfile);
}



您采取了修改缓冲区的建议,并将其与单个

字符打印相结合,以产生相当奇怪的东西。没错,

只是奇怪的。为什么更改缓冲区[count]什么时候不打印

字符如果你不打印呢?

You took a suggestion to modify the buffer and combined it with single
character printing to produce something rather odd. It is not wrong,
just odd. Why change buffer[count] when is it not a printing
character if you don''t then print it?


// fputc (outfile,isprint(buffer [count])?buffer [count]:''。'');

}


/ * for(count = 0; count< sizeof buffer; count ++)

{

(int)fwrite(buffer,1,isprint(buffer [count]),outfile);

} * /


fclose(outfile);

getchar();

返回0;

}


输入文件内容:

Fred Flinstone 123-456-7890

Barney Rubble 123- 789-4561

输出文件:

B12y7p456r
//fputc(outfile, isprint(buffer[count]) ? buffer[count]:''.'');
}

/*for(count = 0; count < sizeof buffer; count++)
{
(int) fwrite(buffer, 1, isprint(buffer[count]), outfile);
}*/

fclose(outfile);
getchar();
return 0;
}

Input file contents:
Fred Flinstone 123-456-7890
Barney Rubble 123-789-4561

Output file:
B12y7p456r



不是我得到的,但仍然。


我应该指出,虽然我只评论了细节,但这个程序的总体设计看起来有些古怪。如果你打算使用
处理文本文件,为什么你有一个117 / 4-1的缓冲区?为什么

当你阅读超过这28个b
字符时,你是否乐意重复使用该空间?如果输入有30个字符,则缓冲区将包含

数字29和30,后跟字符编号3,4等等。它看起来很奇怪。


你应该说出你真正要做的事情。


-

Ben。

Not what I get, but still.

I should point out that although I have only commented on details, the
overall design of this program looks a bit wacky. If you intend to
process text files, why do you have a buffer of size 117/4-1? Why are
you happy to simply reuse the space when you read more than these 28
characters? If the input has 30 characters, your buffer will contain
numbers 29 and 30 followed by character numbers 3, 4 and so on. It
all looks very odd.

You should maybe say what you are actually trying to do.

--
Ben.


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

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