所以我试着读取一个文件并将其存储到我的数据结构中,但是每次运行它都会读取垃圾数据并且我的结构填充为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);
}
}
答案 0 :(得分: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 :(得分:1)
计算行数后,必须再次将文件指针重置为文件的开头。
使用rewind()调用,它会在行计数循环后将文件位置重置为文件启动:
rewind(file);