[ create a new paste ] login | about

Link: http://codepad.org/pfZsf6P4    [ raw code | output | fork ]

C, pasted on Jun 4:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct hoge{
int num;
char name[10];
};
typedef struct hoge hoge_t;

int func1(int, int, hoge_t []);//データ読み込み・構造体への格納処理

//CSVデータ読みこみ
int func1(int year,int month,hoge_t hog[])
{
FILE *fp;
char filename[1024];
char buf[2048];
char *str_loc;
char separator = ',';
int cnt_t = 0;
hog= (hoge_t *)malloc(sizeof(hoge_t)*31);
if (hog==NULL){
printf("メモリ確保失敗しました\n");
return -1;
}
sprintf(filename,"/tmp/%d%d.csv",year,month);
fp = fopen(filename, "r");
if(fp ==NULL){
printf("ファイル読み込みに失敗しました\n");
return -1;
}

while(fgets(buf,2048,fp) != NULL){
str_loc = strrchr(buf,'\n');
*str_loc = 0x00; //行末に\0を代入 
// printf("読み込まれている文字列bufは「%s」<br>\n",buf);
// printf("%d文字\n",strlen(buf));

//hog[cnt_t].nameに値を入れる処理;
str_loc = strrchr(buf, separator); //行末から探し「,」が最初に見つかった位置を返す
*str_loc = 0x00; //最初のカンマを\0に置き換え
strcpy(hog[cnt_t].name,buf+strlen(buf)+1);

//hog[cnt_t].numberに値を入れる処理
hog[cnt_t].num = atoi(buf);

//printf("%d\n",hog[cnt_t].num); //No表示用テスト
//printf("%s\n",hog[cnt_t].name); //名前表示用テスト

cnt_t ++; 
}
fclose(fp);

return 0;
}

 


int main(){
hoge_t hog[31];
int t_year = 2012;
int t_month =6;

func1(t_year, t_month, &hog);

printf("%d%s\n",hog[0].num,hog[0].name);

return 0;
}


Output:
1
2
3
ファイル読み込みに失敗しました
1073742392<


Create a new paste based on this one


Comments: