调用 fscanf 函数时,我的程序挂起。我不知道为什么。我正在尝试从以下文本文件中读取信息:
R1 N001 N003 20
R2 N002 N001 5
R3 N001 0 10
R4 N002 N003 10
R5 N003 N000 5
I1 0 N002 10
这是我的结构:
#include<stdlib.h>
#include<stdio.h>
#ifndef MYDATA_H_
#define MYDATA_H_
typedef struct comp{
char *name;
char *node1;
char *node2;
float val;
} ComponentType;
typedef struct ListNodeT{
ComponentType Component;
float currnet;
float voltage;
float power;
} ListNodeType;
#endif
这是我读取文本文件并填充我的 ComponentType 结构的函数。
void ReadFile (ComponentType *CircuitData, int *numEl, int *numNodes){
*numEl = 0;
*numNodes = 0;
int index= 0;
FILE *myFile = fopen("mydata.dat", "r");
if (myFile == NULL){
printf("Error: File the file is not open");
}
printf("This worked\n"); //gets to this point
while (!feof(myFile)){
fscanf(myFile,"%cf",CircuitData[index].name,
CircuitData[index].node1,CircuitData[index].node2,
&CircuitData[index].val);
printf("\nfscanf works");
*CircuitData[index].node1 = intFromString(CircuitData[index].node1);
*CircuitData[index].node2 = intFromString(CircuitData[index].node2);
if ((int)CircuitData[index].node1 > *numNodes ){
*numNodes = (int)CircuitData[index].node1;
}
if ( (int)CircuitData[index].node2 > *numNodes ){
*numNodes = (int)CircuitData[index].node2;
}
(*numEl)+=1;
index++;
}
fclose(myFile);