让fscanf存储在C中的结构中

让fscanf存储在C中的结构中,c,struct,scanf,C,Struct,Scanf,我有一个任务,我需要从文件中获取RLC电路的值,并计算共振频率。然而,我的问题是,当我使用fscanf函数时,它只读取文件的第一行,其余的显示为零 #include <stdio.h> data int h; typedef struct cct { int code[50]; float R[50]; float L[50]; float C[50]; } CCT; int read(CCT cct[], int n_p, FILE* fp){ cha

我有一个任务,我需要从文件中获取RLC电路的值,并计算共振频率。然而,我的问题是,当我使用fscanf函数时,它只读取文件的第一行,其余的显示为零

#include <stdio.h>
data

 int h;
typedef struct cct
{
  int code[50];
  float R[50];
  float L[50];
  float C[50];
} CCT;

 int read(CCT cct[], int n_p, FILE* fp){
   char temp;

   if(fp==NULL){
       printf("Error\n");
       return -1;
   }
   fscanf(fp,"%d,%f,%e,%e\n", cct[n_p].code, cct[n_p].R,cct[n_p].L, &cct[n_p].C);

}
int main()
{
   FILE* fp = fopen("U://datafile.txt", "rt");
   int i = 0;
   CCT cct[50];
   int size;

   while (!feof(fp)) {
       read(cct, i, fp);
       i++;
   }
   size = i;

   for (i = 0; i < size; ++i)
     printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code[i], cct[i].R[i],
            cct[i].L[i], cct[i].C[i]);

 scanf("%d",&h);
   fclose(fp);
}
如果有人能指出为什么它只得到第一行,我将不胜感激。谢谢

fscanf(fp,"%d,%f,%e,%e", cct[n_p].code, cct[n_p].R,cct[n_p].L, cct[n_p].C);
...
printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code[0], cct[i].R[0], cct[i].L[0], cct[i].C[0]);

也许是下面这样

#include <stdio.h>

typedef struct cct {
  int code;
  float R;
  float L;
  float C;
} CCT;

int h;

int read(CCT cct[], int n_p, FILE* fp){
   char temp;

   if(fp==NULL){
       printf("Error\n");
       return -1;
   }
   fscanf(fp,"%d,%f,%e,%e\n", &cct[n_p].code, &cct[n_p].R, &cct[n_p].L, &cct[n_p].C);
}
int main(){
   FILE* fp = fopen("U://datafile.txt", "rt");
   int i = 0;
   CCT cct[50];
   int size;

   while (!feof(fp)) {
       read(cct, i, fp);
       i++;
   }
   size = i;

   for (i = 0; i < size; ++i)
     printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code, cct[i].R, cct[i].L, cct[i].C);
scanf("%d",&h);
   fclose(fp);
}
#包括
类型定义结构cct{
int代码;
浮子R;
浮子L;
浮点数C;
}CCT;
int-h;
整数读取(CCT CCT[],整数n_p,文件*fp){
焦炭温度;
如果(fp==NULL){
printf(“错误\n”);
返回-1;
}
fscanf(fp、%d、%f、%e、%e\n、&cct[n\u p].code、&cct[n\u p].R、&cct[n\u p].L、&cct[n\u.p].C);
}
int main(){
文件*fp=fopen(“U://datafile.txt”、“rt”);
int i=0;
CCT CCT[50];
整数大小;
而(!feof(fp)){
读取(cct、i、fp);
i++;
}
尺寸=i;
对于(i=0;i
CCT由多个数组组成(您有数组的数组,这对于练习是错误的,但这不是重点),并且您总是写入数组的元素零。例如,cct[n_p].fscanf()中的代码是数组的地址,它与cct[n_p].code[0]的地址相同。然后你在输出循环中打印代码[i],除了i==0外,它是空的。

我将使用
fgets()
到字符串缓冲区,然后在字符串上使用sscanf(),而不是
fscanf()
它往往工作得更可靠。约翰,你介意向我解释一下吗,因为我是编程新手,谢谢。你想要
CCT-CCT[50]?或
CCT-CCT不清楚您试图做什么
fscanf
将只读取struct cct的每个数组字段的第一个元素。如果50个cct中各有50个
code
R
L
C
,则有某种矩形值表,您只读取第一列。然后您正在打印
cct[i].code[i]
,它们是对角线元素。这不可能是对的。@n.m.我试图单独获取每一组值,以便使用它们进行计算,所以最后我得到了一种代码和频率表。谢谢,它成功了。请解释一下原因好吗?谢谢again@user3570628,
fscanf(fp,“%d,%f,%e,%e”,cct[n_p]。code,cct[n_p].R...
的意思是
fscanf(fp,“%d,%f,%e,%e”,&cct[n_p]。code[0],&cct[n_p].R...
#include <stdio.h>

typedef struct cct {
  int code;
  float R;
  float L;
  float C;
} CCT;

int h;

int read(CCT cct[], int n_p, FILE* fp){
   char temp;

   if(fp==NULL){
       printf("Error\n");
       return -1;
   }
   fscanf(fp,"%d,%f,%e,%e\n", &cct[n_p].code, &cct[n_p].R, &cct[n_p].L, &cct[n_p].C);
}
int main(){
   FILE* fp = fopen("U://datafile.txt", "rt");
   int i = 0;
   CCT cct[50];
   int size;

   while (!feof(fp)) {
       read(cct, i, fp);
       i++;
   }
   size = i;

   for (i = 0; i < size; ++i)
     printf("%d,%0.2f,%0.2f,%0.2f\n", cct[i].code, cct[i].R, cct[i].L, cct[i].C);
scanf("%d",&h);
   fclose(fp);
}