user-0 提问: 文件读写问题，求助

回复:
user: user-1
content: 以下代码可以把共视卫星号存入数组：  int main() { 	int n, r; 	int *satelite;  //存放共视卫星号 	int nums;       //共视卫星个数 	double d; 	FILE *f; 	f = fopen("1.txt", "r"); 	n = 0; 	while (1) { 		r = fscanf(f, "%lf", &d); 		if (1 == r) { 			n++; 			printf("[%d]==%lg\n", n, d); 			//根据第一个数字，确定共视卫星个数 			if (1 == n) 			{ 				nums = (int)d; 				satelite = malloc(sizeof(int)*nums); 			} 			//共视卫星号存入数组 			if (2 <= n && n <= nums + 1) satelite[n - 1] = (int)d; 		} 		else if (0 == r) { 			fscanf(f, "%*c"); 		} 		else break; 	} 	fclose(f); 	return 0; }  至于双差距离，那个数字指定了双差距离的个数？

user: user-0
content: 引用 1 楼 zhangxiangDavaid 的回复:借用代码：  #include <stdio.h> int main()  { 	int n, r; 	double d; 	FILE *f; 	f = fopen("1.txt", "r"); 	n = 0; 	while (1) { 		r = fscanf(f, "%lf", &d); 		if (1 == r) { 			n++; 			printf("[%d]==%lg\n", n, d); 		} 		else if (0 == r) { 			fscanf(f, "%*c"); 		} 		else break; 	} 	fclose(f); 	return 0; }   1.txt  共视卫星个数：7 共视卫星号： 6 15 18 21 22 24 27 参考卫星：序号 3卫星号21 双差几何距离： 12814.8 12814.8 12814.8 12814.8 -8532.57 -8532.57 -8532.57 -8532.57 3714.42 3714.42 3714.42   我如果把7赋给一个变量，共视卫星号付给一个数组，下面的也是，该怎么写。我是新手，非常感谢您的帮助！

user: user-1
content: 楼上有bug，更改如下：  int main()  { 	int n, r; 	//int *satelite;  //存放共视卫星号 	int satelite[256];   //预先设定数组大小较好 	int nums;       //共视卫星个数 	double d; 	FILE *f; 	f = fopen("1.txt", "r"); 	n = 0; 	while (1) { 		r = fscanf(f, "%lf", &d); 		if (1 == r) { 			n++; 			printf("[%d]==%lg\n", n, d); 			//根据第一个数字，确定共视卫星个数 			if (1 == n) 			{ 				nums = (int)d; 				if (nums > 256) 				{ 					fprintf(stderr, "too many satelites!\n"); 					fclose(f); 					return 1; 				} 			} 			//共视卫星号存入数组 			if (2 <= n && n <= nums + 1) satelite[n - 1] = (int)d; 		} 		else if (0 == r) { 			fscanf(f, "%*c"); 		} 		else break; 	} 	fclose(f); 	return 0; }

user: user-2
content: http://blog.csdn.net/hong__fang/article/details/43488265

user: user-1
content: 新的文件格式是这样：  共视卫星个数： 7 共视卫星号： 6 15 18 21 22 24 27 参考卫星： 3 序号： 21 双差几何距离： 12814.8 12814.8 12814.8 12814.8 -8532.57 -8532.57 -8532.57 -8532.57 3714.42 3714.42 3714.42

user: user-1

user: user-1
content: 更改文件格式：  /* 共视卫星个数：7 共视卫星号： 6 15 18 21 22 24 27 参考卫星：3 序号：21 双差几何距离： 12814.8 12814.8 12814.8 12814.8 -8532.57 -8532.57 -8532.57 -8532.57 3714.42 3714.42 3714.42 */ int main(void) { 	FILE *f; 	if (NULL == (f = fopen("1.txt", "r"))) 	{ 		fprintf(stderr, "can not open file:%s\n", "1.txt"); 		return 1; 	} 	int satelite[256], n1, n2, d; 	double dis[256]; 	char buf[256]; 	n1 = n2 = 0; 	fgets(buf, 256, f); 	fscanf(f, "%d\n", &d); 	printf("%d\n", d); 	//读一行 	fgets(buf, 256, f); 	//读取共视卫星号 	fgets(buf, 256, f); 	int x = 0; 	while (1) 	{ 		n = sscanf(buf + x, "%d", satelite + n1); 		if (1 == n) 		{ 			printf("%d ", satelite[n1]); 			while (buf[x]) 			{ 				if (' ' == buf[x]) 				{ 					x++; 					break; 				} 				x++; 			} 			if (++n1 >= 256) break; 		} 		else if (0 == n) sscanf(buf, "%*c"); 		else break; 	} 	printf("\n"); 	fgets(buf, 256, f); 	fscanf(f, "%d\n", &d); 	printf("%d\n", d); 	fgets(buf, 256, f); 	fscanf(f, "%d\n", &d); 	printf("%d\n", d);  	//再读一行 	fgets(buf, 256, f); 	//读入双差几何距离 	//fgets(buf, 256, f); 	while (!feof(f)) 	{ 		n = fscanf(f, "%lf", dis + n2); 		if (1 == n) 		{ 			printf("%lg ", dis[n2]); 			n2++; 		} 		else break; 	} 	fclose(f); 	return 0; }

user: user-3
content: 一楼已有！！！

user: user-1
content: 借用代码：  #include <stdio.h> int main()  { 	int n, r; 	double d; 	FILE *f; 	f = fopen("1.txt", "r"); 	n = 0; 	while (1) { 		r = fscanf(f, "%lf", &d); 		if (1 == r) { 			n++; 			printf("[%d]==%lg\n", n, d); 		} 		else if (0 == r) { 			fscanf(f, "%*c"); 		} 		else break; 	} 	fclose(f); 	return 0; }   1.txt  共视卫星个数：7 共视卫星号： 6 15 18 21 22 24 27 参考卫星：序号 3卫星号21 双差几何距离： 12814.8 12814.8 12814.8 12814.8 -8532.57 -8532.57 -8532.57 -8532.57 3714.42 3714.42 3714.42