C - 从文件中读取并保存变量

时间:2016-12-10 15:49:03

标签: c file variables

我从这两个文件中读取问题,文件1和文件2,然后将它们的信息保存在2个变量中。 从文件1我想保存频道的名称,从文件2我想保存用户的名称和他们签名的频道。

我正在考虑创建2个typedef结构(如下图所示)然后创建2个变量(如下所示)并打开文件并将信息放在这些列表中。

我也知道另一种方法就是制作像这个char [100] [100]这样的2D数组,这两种解决方案的唯一问题是我必须对通道数量施加一个上限列表/数组有。

我不确定这些是否是最好的方法,或者如果有更好更简单的方法,你们可以帮忙吗?

如果你们需要更多信息,请说明,谢谢!

Edit1:我已经添加了我现在拥有的file1代码的读取数据,我认为它似乎正常工作,但我的问题/问题更多的是将信息保存到的正确方法变量还是有更好/更简单的方法吗?感谢。

Channel channels[MAX_CHANNELS];

Registration registrations[MAX_REGISTRATIONS];

typedef struct{
    char name_channel[20];
    int id;
} Channel;

typedef struct{
    char username[50];
    char name_channel[20];
} Registration;

File1中:

General
SO
PCD
FBD

文件2:

2016-09-26 14:00:01 paul General
2016-09-26 14:01:11 mary SO
2016-09-27 10:33:17 paul SO
2016-09-27 13:32:10 rachel General
2016-09-27 13:32:12 rachel FBD

读取文件的代码(我还没有完成文件1)。

    File *file1 = fopen("channels.txt", "r");
    if(file1==NULL){ perror("Reading error: "); exit(1); } ;

    char line[100];
    int i = 0;
    int w=0;
    for(w;w<MAX_CHANNELS;w++){
        channels[w].id=-1;
        strcpy(channels[w].name, "n");
    }
    while(fgets(line, 100, file1) != NULL){

        printf("Line read: %s", line);   
        line[ strlen(line) -1 ] = 0;
        Channel a;
        strcpy(a.name , line);
        a.id=1;
        channels[i]=a;
        i++;
    }

    fclose(canais);
    int k;
    for(k=0; k<MAX_CHANNELS; k++){
        if(channels[k].id!=-1)
            printf("testing var with channels: %s\n", channels[k].name);
    }

1 个答案:

答案 0 :(得分:1)

一些可能有用的提示(在代码注释中):)我认为你的方式很好。我认为这也是可扩展的,因为如果想要进一步丰富数据,可以向结构中添加新成员。我见过strtok用来解析数据很多。由于它的工作方式,Strtok应该不再需要覆盖换行符。

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

#define MYSTRINGLEN (50) // In general "magic numbers" 
//in code makes it hard to read, though these are fairly apparent, 
//but try using #define where you can

typedef struct {
  char name[MYSTRINGLEN];
//  int id;
}Channel;

typedef struct {
  char username[MYSTRINGLEN];
  char name[MYSTRINGLEN];
} Registration;


int main(int argc, char *argv[]){
  int i = 0;
  //int w = 0; not needed
  int k = 0;
  char line[100];

  Channel channels[BUFSIZ] = {{{0}}}; // num of brackets depends on num of nested data structure
  Registration registrations[BUFSIZ] = {{{0}}};

/* init all to zero instead w/ bracket syntax
  for (w = 0; w < BUFSIZ; w++){ 
    channels[w].id = -1;
    strcpy(channels[w].name, "n");
  }
*/
  FILE * file1 = fopen("channels.txt", "r");
//Many people use strtok to get done what you are doing here if you are interested
  while(fgets(line,100,file1)){  // do not need to explicitly state NULL
    printf("Line read %s\n", line);
    line[strlen(line) -1] = 0;
    //Channel a; You have already initialized a whole array of struct, just use them
    strcpy(channels[i].name, line);
    //a.id = 1;
    //channels[i]=a;
    i++; 
  }

  fclose(file1);

  for(k = 0; k < BUFSIZ; k++){
    if (0 != channels[k].name[0]){ //can test if string was populated, dont need id flag
      printf("testing var with channels: %s\n", channels[k].name);
    }
  }  

  return 0;
}
de here