2013-12-07 129 views
0

所以我试图读取一个文件并将其存储到我的数据结构中,但每次运行它时,它都会读入垃圾数据,并且结构中充满了0。有什么建议么?如何读取文件并存储它?

我有函数来检查数据是否有效,因为我的结构不能存储它已存储的数据(例如相同的端口或vmn)。

#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <stdbool.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 


typedef struct DataType{ 
    double timeOffset; 
    int vmn; 
    int port; 
}Data; 

void insertDataType(Data *Data, double timeOffset, int vmn, int port){ 
    Data->timeOffset = timeOffset; 
    Data->vmn = vmn; 
    Data->port = port; 
} 

double returnTimeOffset(Data D){ 
    assert(D.timeOffset != 0.0); 
    return D.timeOffset; 
} 

int returnVMN(Data D){ 
    assert(D.vmn != 0); 
    return D.vmn; 
} 

int returnPort(Data D){ 
    assert(D.port != 0); 
    return D.port; 
} 

bool vmnValid(Data *Data, int n, int vmn){ 
    int i; 
    for(i = 0; i <= n; i++){ 
     if(Data[i].vmn != 0){ 
      if(Data[i].vmn == vmn){ 
       printf("Invalid vmn %d: vmn already inserted \n", vmn); 
       return false; 
      } 
     } 
    } 
    return true; 
} 

bool timeValid(Data *Data, int n, double timeOffset){ 
    int i; 
    for(i = 0; i <= n; i++){ 
     if(Data[i].timeOffset != 0.0){ 
      if(Data[i].timeOffset == timeOffset){ 
       printf("Invalid timeOffset %2lf: timeOffset already used \n", timeOffset); 
       return false; 
      } 
     } 
    } 
    return true; 
} 

bool portValid(Data *Data, int n, int port){ 
    int i; 
    for(i = 0; i <= n; i++){ 
     if(Data[i].port != 0){ 
      if(Data[i].port == port){ 
       printf("Invalid port %d: port already in use\n", port); 
       return false; 
      } 
     } 
    } 
    return true; 
} 


int main(int argc, const char * argv[]){ 
    int n = 0; 
    int i = 0; 
    char c; 
    FILE *file; 

    // Open file 
    file = fopen("connect1.in", "r"); 
    if (file == NULL) { 
     fprintf(stderr, "Invalid input file \n"); 
     exit(1); 
    } 

    // Get number of lines (n) 
    while((c = fgetc(file))!= EOF){ 
     if(c == '\n'){ 
      n++; 
     } 
    } 

    printf("n = %d \n", n); 

    // Create a strut DataType of size n 
    Data *storage; 
    storage = calloc(n, sizeof(struct DataType)); 

    // Read and insert the data 
    double timeOffset; 
    int vmn; 
    int port; 

    printf("\n"); 
    while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port != EOF)){ 
     printf("%lf %d %d \n", timeOffset, vmn, port); 
     if(timeValid(storage, n, timeOffset)){ 
      if(vmnValid(storage, n, vmn)){ 
       if(portValid(storage, n, port)){ 
        insertDataType(&storage[vmn], timeOffset, vmn, port); 
       } 
      } 
     } 
    } 
    printf("\n"); 

    printf("\n"); 
    printf("Storage:\n"); 
    for(i = 0; i <= n; i++){ 
     printf("%3d: %2lf %d %d \n", i, storage[i].timeOffset, storage[i].vmn, storage[i].port); 
    } 
} 

回答

1

变化

// while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port != EOF)){ 
while(fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port) != EOF){ 
// or better 
while (fscanf(file, "%lf,%d,%d,", &timeOffset, &vmn, &port) == 3) { 

@suspectus是正确的,加rewind(file);

// change 
// char c; 
int c; 

次要的考虑:

Data *storage; 
// storage = calloc(n, sizeof(struct DataType)); 
// I like the style 
storage = calloc(n, sizeof(*storage)); 

// In a number of places, function do not change *Data, so use `const` 
// Useful to now, at a glance, that *Data is unchanged 
// and forces the compiler to warn otherwise. 
// bool vmnValid(Data *Data, int n, int vmn){ 
bool vmnValid(const Data *Data, int n, int vmn) { 
1

计算完行数后,文件指针必须重新设置为文件的开始位置。

使用倒带()调用,它会重置文件位置回行计数循环后,文件开始:

rewind(file); 
+0

我觉得这是什么固定它。非常感谢你。 – CGTheLegend

+0

不客气。不要忘记将答案打勾为可接受的解决方案,谢谢。 – suspectus

相关问题