读一个CSV文件导入结构数组 [英] reading a csv file into struct array
问题描述
我开始在C code $我的C $ c是如下:
I'm beginning to code in C. My code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 256
#define MAX_BOOKS 256
struct book{
int ID;
char *name;
char *dateIn;
char *dateOut;
};
struct book books[MAX_BOOKS];
/* PROTOTYPE OF FUNCTIONS */
int readBookFile();
void printBookList();
int main(int argc, char **argv)
{
int isOK = 0;
isOK = readBookFile();
printBookList();
system("pause");
return 0;
}
int readBookFile()
{
/* FileStream for the Library File */
FILE *bookFile;
/* allocation of the buffer for every line in the File */
char *buf = malloc(MAX_STR_LEN);
char *tmp;
/* if the space could not be allocaed, return an error */
if (buf == NULL) {
printf ("No memory\n");
return 1;
}
if ( ( bookFile = fopen( "library.dat", "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
}
int i = 0;
while (fgets(buf, 255, bookFile) != NULL)
{
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ";");
books[i].ID = atoi(tmp);
tmp = strtok(NULL, ";");
books[i].name = tmp;
tmp = strtok(NULL, ";");
books[i].dateIn = tmp;
tmp = strtok(NULL, ";");
books[i].dateOut = tmp;
//tempBook.ID = atoi(buf);
printf("index i= %i ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);
i++;
}
//free(buf);
fclose(bookFile);
return 0;
}
void printBookList()
{
int i;
//i = sizeof(books) / sizeof(books[0]);
//printf ("%i \n", i);
for (i = 0; i <= sizeof(books); i++)
{
if (books[i].ID != 0)
printf("index i= %i ID: %i, %s, %s, %s \n",i, books[i].ID , books[i].name, books[i].dateIn , books[i].dateOut);
else
break;
}
}
问题是,readBookFile()结束后,我的结构的阵列是满的输入文件的最后一个值。
The problem is, that after readBookFile() ends, the Array of my struct is full of the last value of the input file..
我的输入文件是:
1;das erste Buch; 12122013; 13122013
2;das Zweite Buch; 12122013; 13122013
3;das dritte Buch; 12122013; 13122013
4;das vierte Buch; 12122013; 13122013
5;das fünfte Buch; 12122013; 13122013
6;das sechste Buch; 12122013; 13122013
所以在readBookFile起作用的printf的返回正确的值,但在printBooksList()函数的所有值似乎已经改变到我inputfile的最后一行。
so in the readBookFile function the printf returns the correct values, but in the printBooksList() function all values seem to have changed to the last line of my inputfile.
任何人能解释这个给我,也许我指出了正确的方向?
Can anyone explain this to me and maybe point me in the right direction?
非常感谢
Hagbart
Thanks a lot Hagbart
推荐答案
在while循环:
while (fgets(buf, 255, bookFile) != NULL)
您复制到文件,从缓冲新内容的存储位置。如TMP指向在缓冲器的某一点时,其内容被替换太
you are copying into the memory location of buffer new contents from file. As tmp points to a certain point in the buffer, its contents are being replaced too.
tmp = strtok(NULL, ";");
books[i].name = tmp;
您应该对数组的每个结构分配内存,然后使用strcopy。
You should allocate memory for each struct of the array and then use strcopy.
您可以找到的strcpy的strdup和这里的区别的一个解释:
的strcpy VS的strdup
You can find an explanation of differences between strcpy and strdup here: strcpy vs strdup
这篇关于读一个CSV文件导入结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!