- 相關(guān)推薦
C語言編程題
1)讀文件file1.txt的內(nèi)容(例如):
12
34
56
輸出到file2.txt:
56
34
12
(逆序)
第一題,注意可增長數(shù)組的應(yīng)用.
#include
#include
int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
FILE *fp1;
FILE *fp2;
fp1 = fopen(“a.txt”,”r”);
if(fp1 == NULL)
{printf(“error1″);
exit(-1);
}
fp2 = fopen(“b.txt”,”w”);
if(fp2 == NULL)
{printf(“error2″);
exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,”%d”,&a[i]) != EOF)
{
i++;
j++;
if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
printf(“error3″);
exit(-1);
}
a = b;
}
}
for(;–j >= 0;)
fprintf(fp2,”%d\n”,a[j]);
fclose(fp1);
fclose(fp2);
return 0;
}
可謂是反序的經(jīng)典例程.
void inverse(char *p)
{
if( *p = = ‘\0′ )
return;
inverse( p+1 );
printf( “%c”, *p );
}
int main(int argc, char *argv[])
{
inverse(“abc\0″);
return 0;
}
借簽了樓上的“遞規(guī)反向輸出”
#include
void test(FILE *fread, FILE *fwrite)
{
char buf[1024] = {0};
if (!fgets(buf, sizeof(buf), fread))
return;
test( fread, fwrite );
fputs(buf, fwrite);
}
int main(int argc, char *argv[])
{
FILE *fr = NULL;
FILE *fw = NULL;
fr = fopen(“data”, “rb”);
fw = fopen(“dataout”, “wb”);
test(fr, fw);
fclose(fr);
fclose(fw);
return 0;
}
在對齊為4的情況下
struct BBB
{
long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0×1000000;
p+0×200=____;
(Ulong)p+0×200=____;
(char*)p+0×200=____;
希望各位達人給出答案和原因,謝謝拉
解答:假設(shè)在32位CPU上,
sizeof(long) = 4 bytes
sizeof(char *) = 4 bytes
sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes
【C語言編程題】相關(guān)文章:
c 面試編程問題08-11
C/C++筆試題目大全08-22
VB編程:使用枚舉的情況05-15
《維生素c的故事》教學(xué)反思10-14
C++面試筆試題目07-16
IQ題筆試題07-06
家樂福筆試真題07-01
阿里巴巴C++工程師筆試題目10-17
筆試中的硬件類題09-24