c语言将txt文件换行
发布网友
发布时间:2023-07-03 23:40
我来回答
共2个回答
热心网友
时间:2023-09-15 14:31
中文字符,一个字是二个字节!
#include <stdio.h>
#include <string.h>
int main( void )
{
FILE* in;
FILE* out;
char cin;
int nLines = 0,i=0;
int bLow = 0;
if( ( in = fopen( "1.txt", "r" ) ) == NULL )
{
printf( "error can not read 1.txt.\npress any key to continue..." );
getchar();
return 1;
}
if( ( out = fopen( "2.txt", "a" ) ) == NULL )
{
printf( "error can not open 2.txt.\npress any key to continue..." );
getchar();
return 1;
}
while( !feof(in) )
{
cin = fgetc( in );
if(cin == ' ')
continue;
if(cin == '\n')
continue;
if(cin < 0)
{//是中文字符
if(bLow)
{
bLow=0;
}
else
{
nLines++;
bLow=1;
}
}
else
{
bLow=0;
if(bLow)
{
printf( "error file Fomat!" );
break;
}
nLines++;
}
if(nLines==10)
{
fputc( '\r', out );
fputc( '\n', out );
nLines=0;
}
fputc( cin, out );
}
fputc( '\r', out );
fputc( '\n', out );
fclose( in );
fclose( out );
}
热心网友
时间:2023-09-15 14:32
中文字符,一个字是二个字节!
#include
<stdio.h>
#include
<string.h>
int
main(
void
)
{
FILE*
in;
FILE*
out;
char
cin;
int
nLines
=
0,i=0;
int
bLow
=
0;
if(
(
in
=
fopen(
"1.txt",
"r"
)
)
==
NULL
)
{
printf(
"error
can
not
read
1.txt.\npress
any
key
to
continue..."
);
getchar();
return
1;
}
if(
(
out
=
fopen(
"2.txt",
"a"
)
)
==
NULL
)
{
printf(
"error
can
not
open
2.txt.\npress
any
key
to
continue..."
);
getchar();
return
1;
}
while(
!feof(in)
)
{
cin
=
fgetc(
in
);
if(cin
==
'
')
continue;
if(cin
==
'\n')
continue;
if(cin
<
0)
{//是中文字符
if(bLow)
{
bLow=0;
}
else
{
nLines++;
bLow=1;
}
}
else
{
bLow=0;
if(bLow)
{
printf(
"error
file
Fomat!"
);
break;
}
nLines++;
}
if(nLines==10)
{
fputc(
'\r',
out
);
fputc(
'\n',
out
);
nLines=0;
}
fputc(
cin,
out
);
}
fputc(
'\r',
out
);
fputc(
'\n',
out
);
fclose(
in
);
fclose(
out
);
}