方式一:-----------含有多行,每行含有一列数据
int FileReader(char * file,double * a)
{
FILE * fp1 = fopen(file, "r");//打开输入文件
if (fp1==NULL) {//若打开文件失败则退出
puts("不能打开文件!");
return 0;
}
for(int i=0;fscanf(fp1,"%lf",a+i)!=EOF;i++);//从输入文件连续读取浮点型数到数组a
fclose(fp1);//关闭输入文件
return 1;
}
方式二:-----------含有多行,每行含有多列数据
char * topo[5000]; //将topo赋给buff;
int spec = 5000; //表示行数
#denfine MAX_LINE_LEN 4000
int read_file(char ** const buff, const unsigned int spec, const char * const filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return 0;
}
// PRINT("Open file %s OK.\n", filename);
char line[MAX_LINE_LEN + 2];
unsigned int cnt = 0;
while ((cnt < spec) && !feof(fp))
{
line[0] = 0;
fgets(line, MAX_LINE_LEN + 2, fp);
if (line[0] == 0) continue;
buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2);
strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1);
buff[cnt][4001] = 0;
cnt++;
}
fclose(fp);
return cnt;
}
//对topo进行操作,取出每一列的数据
int edge_num = 5000;
char * delim = ",|\n"; //以制定的格式进行列的分割
char *p = NULL;
int i = 0, j = 0;
int value = 0;
for(i=0; i<edge_num; i++) //对每一行进行操作
{
memcpy(buffer, topo[i], strlen(topo[i]));
j = 0;
p = strtok(buffer, delim); //用到字符串的分割函数
//while(NULL != p)
while(4 != j) //需要分割的列书为4行
{
value =atoi(p);
if(0 == j%4)
{
LinkID = value; //取得第一列的数字
}
j++;
p = strtok(NULL, delim);
}