|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char s[500];
char* mozi[300];
int suuzi[300];
int i;
int n;
FILE *fp;
if ( (fp = fopen("test.txt","r")) == NULL) {
printf("ファイルが開けません\n");
return 1;
}
i = 0;
n = 0;
while ( (fgets(s,256,fp)) != NULL ){
/* 改行文字を取り除く */
char* tp = strchr(s,'\n');
if ( tp != NULL ) {
*tp = '\0';
}
/* 最初の文字が数字ならば数字列とみなす */
if ( isdigit(s[0]) != 0 ) {
suuzi[i] = atoi(s);
i++;
} else {
mozi[n] = _strdup(s); /* 複製を作る */
n++;
}
}
fclose(fp);
{
int t;
printf("--- strings ---\n");
for ( t = 0; t < n; t++ ){
printf("%s\n",mozi[t]);
free(mozi[t]); /* 複製を破棄 */
}
printf("--- numbers ---\n");
for( t = 0; t < i; t++){
printf("%d\n",suuzi[t]);
}
}
return 0;
}
|