2015-11-20 142 views
-2

我有一个文本文件中包括这些原糖:我怎样才能把这些放在一个数组中?

123#john#smith 
153#jane#smith 

我割裂开来,写这样的:

123 
john 
smith 
153 
.. 

现在我要追究他们在二维数组。我能怎么做?

void read_file() 
{            
FILE *file; 
char name[100]; 
char *p; 
file=fopen("names.txt","r"); 
while(!feof(file)) 
{ 

    fscanf(file,"%s\n",name); 
    p= strtok(name," #"); 
     while(p!=NULL) 
     { 
      printf("%s\n",p); 
      p= strtok(NULL, " #"); 
     } 
    } 
fclose(file); 
    } 
+0

您可以创建一个结构来表示每个“行”,然后创建一个这样的数组? –

+0

@Nodeum:请学会像开发人员一样思考。不要说*它会给出错误*,显示你是如何做到的,并显示错误是什么。这是基本的东西。 Stackoverflow不是人们猜测你做了什么以及出了什么问题的地方。 –

回答

0

像@jeff carey说的,你可以使用struct。因为你想操纵不同类型的数据。 下面是一个例子:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

/* The struct */ 
typedef struct 
{ 
    int number; 
    char* firstName; 
    char* lastName; 
} Name; 

int main(void) 
{            
    FILE *file; 
    char buffer[100]; 
    char *p; 
    int i; 
    Name myNames[2]; 

    file = fopen("names.txt","r"); 

    for(i = 0; i < 2; i++) 
    { 
     fscanf(file,"%s\n",buffer); 

     p = strtok(buffer," #"); 
     myNames[i].number = atoi(p); 

     p = strtok(NULL," #"); 
     myNames[i].firstName = strdup(p); 

     p = strtok(NULL," #"); 
     myNames[i].lastName = strdup(p); 
    } 

    fclose(file); 

    /* Printing the data. */ 
    for(i = 0; i < 2; i++) 
    { 
     printf("Number: %d. First Name: %s. Last Name: %s.\n", myNames[i].number, myNames[i].firstName, myNames[i].lastName); 
    } 

    /* strdup allocates memory on heap, and you should free it. */ 
    for(i = 0; i < 2; i++) 
    { 
     free(myNames[i].firstName); 
     free(myNames[i].lastName); 
    } 

    return 0; 
} 
0

你有一个文件,其中包含限定值的数组/列表(按“#”)文本,

123#john#smith 
153#jane#smith 
173#joe#doe 
... 

你可以精心字符数组数组来保存每个字段/列,假设您事先知道列有多大。但是你可能无法确定尺寸,而且这确实不是最好的方式。你可以定义一个结构,但同样的问题。

因此,为文件的每一行定义一个char *数组,您既可以指向数据的指针,也可以使用strdup为数据分配新的缓冲区。

#define MAXFIELDS (20) /*pick a sane value*/ 
char** /*return an array of pointers to char, char* fields[] */ 
parse_line(char* line,int* count) 
{ 
    int ndx; 
    char* p=line; 
    char* element; 
    char** fields = malloc(MAXFIELDS*sizeof(char*)); 
    for(ndx=0; element = strtok(p,"#"); ++ndx) { 
     p=NULL; 
     /*either point directly at (a copy of) the string*/ 
     fields[ndx] = element; 
     /*or use strdup to make a copy*/ 
     fields[ndx] = strdup(element); 
    } 
    /*ndx contains number of elements*/ 
    if(count) *count=ndx; 
    return(fields); 
} 

您可以定义行的最大数量从文件中读取,也可以使用链表(使用固定长度的数组在这里为简便起见)。

#define MAXROWS (100) /*pick a sane value*/ 
char** rows[MAXROWS]; 

void read_file() 
{            
    FILE *file; 
    char name[100]; 
    char *p; 
    int ndx=0; //rowcount 
    int fieldcount; 
    file=fopen("names.txt","r"); 
    while(!feof(file)) { 
     fscanf(file,"%s\n",name); 
     /* either make a copy of the line here, or copy elements inside parse_line*/ 
     p = strdup(name); 
     rows[ndx++] = parse_line(p,&fieldcount); 
    } 
    fclose(file); 
} 
相关问题