前几天去面试,碰到一道文件加密题目,供网友参考,其要求如下:
1)对文件的内容取反
2) 如果文件大小为奇数则在文件后面追加一个'B'字符。
3)以文件的每两个字节为一组,对每一组的字节交换。
前提:
/*******************************************************************
文件名:file_ope.c
原始文件:xiong.c 设文件内容为:1234567890
加密后存入文件:feng.c
编辑环境:ubuntu 6.10 vim
*******************************************************************/
源代码:
#include <stdio.h>
#include <string.h>
int readfile(char *filename, char *buf);
int writefile(char *filename, char *buf, int length);
long filelength(char *filename);
int appendfile(char *filename, char *buf);
int main(int argc, char **argv)
{
int ret = 0;
int ret1 = 0;
int i = 0;
int len = 0;
char b = 'B';
char swap;
long length = 0;
char buf[1024] = "";
/*****对文件xiong.c的每一个字节取反,并写入文件feng.c中*****/
ret = readfile("xiong.c", buf);
if (!ret) {
printf("\tread file xiong.c content sucess!\n");
} else {
printf("\tread file xiong.c content fail!\n");
}
printf("\tbuf = %s", buf); /*********????************/
while (buf[i] != '\0') {
buf[i] = ~buf[i];
i++;
}
len = strlen(buf);
ret1 = writefile("feng.c", buf, len);
if (ret1) {
printf("\twrite file feng.c content sucess!\n");
} else {
printf("\twrite file feng.c content fail!\n");
}
/**判断文件feng.c的大小是偶数还是奇数,如是奇数则在文件feng.c中追加'B'**/
length = filelength("feng.c"); //通过fseek和ftell函数求出文件的大小
printf("\tfeng.c length = %ld\n", length);
if (length % 2 == 0) {
printf("\tThe length is ou shu!\n");
} else {
if (appendfile("feng.c", &b)) {
printf("\twrite 'B' sucess\n");
} else {
printf("\twrite 'B' fail\n");
}
}
length = filelength("feng.c"); //测试加'B'后文件feng.c是否为偶数
printf("\tAfter append 'B' of the file feng.c'length = %ld\n", length);
/**以每两个字节为一组,使这组中的两个字节互换,再写入feng.c中**/
memset(buf, 0, sizeof(buf));
ret = 1;
ret = readfile("feng.c", buf);
if (!ret) {
printf("\tread file xiong.c content sucess!\n");
} else {
printf("\tread file xiong.c content fail!\n");
}
printf("\tbuf = %s\n", buf); /******????******/
while (length!=0)
{
i = 0;
swap = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = swap;
i = i + 2;
length--;
}
ret1 = 0;
len = strlen(buf);
ret1 = writefile("feng.c", buf, len);
if (ret1) {
printf("\twrite file feng.c content sucess!\n");
} else {
printf("\twrite file feng.c content fail!\n");
}
return 0;
}
/**************************读文件函数****************************/
int readfile(char *filename, char *buf)
{
FILE *fp;
int j = 0;
size_t i;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("\topen file error!\n");
} else {
while (1) {
i = fread(&buf[j], 1, 1, fp);
if (i == 0)
break;
j++;
}
}
fclose(fp);
return i;
}
/**************************写文件函数****************************/
int writefile(char *filename, char *buf,int length)
{
FILE *fp;
int j = 0;
size_t i = 0;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("\topen file error!\n");
} else {
while (length != 0) {
i = fwrite(&buf[j], 1, 1, fp);
length--;
j++;
}
}
fclose(fp);
return i;
}
/***************************追加字符'B'函数*********************/
int appendfile(char *filename, char *buf)
{
FILE *fp;
size_t i = 0;
fp = fopen(filename, "a");
if (fp == NULL) {
printf("\topen file error!\n");
} else {
i = fwrite(buf, 1, 1, fp);
}
fclose(fp);
return i;
}
/***************************求文件大小函数*********************/
long filelength(char *filename)
{
FILE *fp;
long len = 0;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("\topen file error!\n");
} else {
len = fseek(fp, 0L, SEEK_END);
printf("1len=%ld\n", len);
len = ftell(fp);
printf("2len=%ld\n", len);
}
fclose(fp);
return len;
}
/********************************结束************************/
编译:gcc -Wall file_ope.c
运行:. /a.out
结果:
read file xiong.c content sucess!
buf = 1234567890
write file feng.c content sucess!
1len=0
2len=11
feng.c length = 11
write 'B' sucess
1len=0
2len=12
After append 'B' of the file feng.c'length = 12
read file xiong.c content sucess!
buf = ??????????
write file feng.c content sucess!
疑问:代码中有两个加了/**********************????******************/的printf语句,为什么一个要'\n',一个不要'\n',我找了好久的原因都没找到,有知道的麻烦告诉我下哦!:)
还有一道关于内存改错的题目,问在主函数中运行是否有错,错在哪?
前题:
/************************************************************************
文件名:memory_error.c
编辑环境:ubuntu 6.10 vim
************************************************************************/
源代码:
void func(void)
{
char a[]="abcdef";
a[0] = 'X';
char *p="123456";
p[0] = 'X';
printf("p = %s\n",p);
printf("a = %s\n",a);
}
调试程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(void);
int main(int argc,char **argv)
{
func();
return 0;
}
void func(void)
{
char a[]="abcdef";
printf("before a[]\n");
a[0] = 'X';
printf("before *p\n");
char *p="123456";
printf("after *p\n");
p[0] = 'X'; //p指向的是一个常量,不能改变它的值
printf("before printf\n");
printf("p = %s\n",p);
printf("a = %s\n",a);
}
编译:gcc -Wall memory_error.c
运行:. /a.out
结果:
before a[]
before *p
after *p
段错误 (core dumped)
错误出在 p[0] = 'X'; 处,上面已经解释了原因。
我改后的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(void);
int main(int argc,char **argv)
{
func();
return 0;
}
void func(void)
{
char a[]="abcdef";
printf("before a[]\n");
a[0] = 'X';
printf("before *p\n");
char *p = NULL;
p = malloc(10);
strcpy(p,"123456");
printf("after *p\n");
p[0] = 'X';//p指向的是一个常量,不能改变它的值
printf("before printf\n");
printf("p = %s\n",p);
printf("a = %s\n",a);
}
编译:gcc -Wall memory_error.c
运行:. /a.out
结果:
before a[]
before *p
after *p
before printf
p = X23456
a = Xbcdef
/********************End************************/
--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/
没有评论:
发表评论