我有一些麻煩用鏈表傳遞一個.csv文件的信息,一個struct傳遞信息通過文件的.csv一個struct ...C語言:使用鏈表
的文件就像:
1,3.234,hello,f
3,4.2432,worldkind,o
.
.
.
我的C計劃:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
typedef struct TP
{
int key;
//data
int atomnum;
float massnum;
char nome[MAX+1];
char sym[MAX+1];
struct tableper *nseg;
struct tableper *nant;
}tp;
tp* putmemory(tp *dbase1) //function to put data to struct
{
FILE *fp1;
char *token,ch,temp[MAX];
int *tokenint,i,aux[MAX],in=-1,flag=0;
char BUFFER[MAX],*aux2;
tp *nv;
if ((fp1 = fopen("TabelaPeriodica.csv","r")) == NULL)
printf("File could not be opened \n");
while(!feof(fp1))
{
while((ch=fgetc(fp1)) != '\r')
{
temp[++in]=ch;
if(ch == ',')
{
nv->key = ++i;
token = strtok(temp[++i],",");
nv->atomnum = token;
token = strtok(NULL,",");
nv->massnum = atof(token);
token = strtok(NULL,",");
strcpy(nv->nome,token);
token = strtok(NULL,",");
strcpy(nv->sym,token);
free(nv);
}
}
}
}
int main()
{
tp *dbase1;
putmemory(dbase1);
return 0;
}
我的問題是:
- 通過字符的向量串
- 的向量讀每一行,並在結構體保存
- 每次迭代節省NV(new_node)使用鏈表
歡迎堆棧溢出:) – pmg 2011-12-25 10:53:55
'而((CH =龜etc(FP1))!= '\ r')'只適用於Windows。我建議你用'while((ch = fgetc(fp1))!='\ n')'代替它,它在Windows和所有其他操作系統上都能很好地工作。 – pmg 2011-12-25 10:59:04
什麼是struct tableper?這是什麼部分應該是基於鏈表的? – Dave 2011-12-25 12:13:24