C语言编程题
1)读文件file1.txt的'内容(例如):
12
34
56
输出到file2.txt:
56
34
12
(逆序)
上海龙凤1314 shlf 第一题,注意可增长数组的应用.
上海龙凤1314 shlf #include
#include
上海龙凤1314 shlf int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
上海龙凤1314 shlf FILE *fp1;
上海龙凤1314 shlf FILE *fp2;
上海龙凤1314 shlf fp1 = fopen(“a.txt”,”r”);
上海龙凤1314 shlf if(fp1 == NULL)
上海龙凤1314 shlf {printf(“error1″);
exit(-1);
}
fp2 = fopen(“b.txt”,”w”);
上海龙凤1314 shlf if(fp2 == NULL)
上海龙凤1314 shlf {printf(“error2″);
上海龙凤1314 shlf exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,”%d”,&a[i]) != EOF)
{
i++;
j++;
上海龙凤1314 shlf if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
上海龙凤1314 shlf printf(“error3″);
上海龙凤1314 shlf exit(-1);
}
a = b;
}
}
上海龙凤1314 shlf for(;–j >= 0;)
fprintf(fp2,”%d\n”,a[j]);
fclose(fp1);
上海龙凤1314 shlf fclose(fp2);
return 0;
}
可谓是反序的经典例程.
上海龙凤1314 shlf void inverse(char *p)
{
if( *p = = ‘\0′ )
return;
inverse( p+1 );
上海龙凤1314 shlf printf( “%c”, *p );
}
int main(int argc, char *argv[])
{
inverse(“abc\0″);
return 0;
}
借签了楼上的“递规反向输出”
#include
上海龙凤1314 shlf void test(FILE *fread, FILE *fwrite)
{
char buf[1024] = {0};
if (!fgets(buf, sizeof(buf), fread))
return;
test( fread, fwrite );
上海龙凤1314 shlf fputs(buf, fwrite);
}
上海龙凤1314 shlf int main(int argc, char *argv[])
{
上海龙凤1314 shlf FILE *fr = NULL;
FILE *fw = NULL;
上海龙凤1314 shlf fr = fopen(“data”, “rb”);
fw = fopen(“dataout”, “wb”);
test(fr, fw);
fclose(fr);
fclose(fw);
return 0;
}
上海龙凤1314 shlf 在对齐为4的情况下
struct BBB
{
long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0×1000000;
上海龙凤1314 shlf p+0×200=____;
(Ulong)p+0×200=____;
(char*)p+0×200=____;
希望各位达人给出答案和原因,谢谢拉
解答:假设在32位CPU上,
sizeof(long) = 4 bytes
上海龙凤1314 shlf sizeof(char *) = 4 bytes
上海龙凤1314 shlf sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes
【C语言编程题】相关文章:
1.c语言面试编程题
2.笔试题(C编程)
3.c 面试编程问题
7.搜狐编程笔试真题