
C语言(文件操作)
题目:请用户在命令行中输入两个文件名,读取出第二个文件的后10个字符,接到第一个文件的末尾。若第二个文件不足10个字符,则给出出错信息。另外请告诉我在cmd里应该用什么格...
题目:请用户在命令行中输入两个文件名,读取出第二个文件的后10个字符,接到第一个文件的末尾。
若第二个文件不足10个字符,则给出出错信息。
另外请告诉我在cmd里应该用什么格式输入这些信息 展开
若第二个文件不足10个字符,则给出出错信息。
另外请告诉我在cmd里应该用什么格式输入这些信息 展开
1个回答
展开全部
#include <stdio.h>
int main()
{
char file1[512], file2[512], str[11];
FILE *fp1, *fp2;
printf( "输入2个文件名,中间空格隔开:" );
scanf( "%s %s", file1, file2 );
if ( (fp1=fopen(file1,"ab+")) == NULL )
{
printf( "open %s error\n", file1 );
return 1;
}
if ( (fp2=fopen(file2,"rb")) == NULL )
{
printf( "open %s error\n", file2 );
return 1;
}
long savePos = ftell( fp2 );
fseek( fp2, 0, SEEK_END );
long fileLength = ftell( fp2 );
fseek( fp2, savePos, SEEK_SET );
if ( fileLength < 10 )
{
printf( "%s 长度不足10\n", file2 );
return 1;
}
fseek( fp2, -10, SEEK_END );
fread( str, 10, 1, fp2 );
str[10] = 0;
printf( "str:[%s]\n", str );
fprintf( fp1, "%s", str );
fclose( fp1 );
fclose( fp2 );
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询