C - 从文件读取并将信息保存在变量上

标签 c file variables

我在读取下面这两个文件(file1 和 file2),然后将它们的信息保存在 2 个变量中时遇到问题。 我想从文件 1 中保存 channel 名称,从文件 2 中我想保存用户的名称及其签名的 channel 。

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

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

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

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

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;

文件1:

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

读取文件的代码(我只完成了file1)。

    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);
    }

最佳答案

只是一些可能有帮助的提示(在代码注释中):) 我认为你这样做的方式很好。我认为这也是可扩展的,因为如果您想进一步丰富数据,您可以向结构添加新成员。我经常看到 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

关于C - 从文件读取并将信息保存在变量上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077315/

相关文章:

c++ - 在 Windows 中使用 UTF8 字符串打开文件名

mysql - 在 shell 脚本中将表计数设置为变量..?

variables - 变量的不同行为和函数的返回值

c - 如何在 C 中进行合格的导入?

c - 将 pthread id 作为输入

c++ - C - PJLIB 为什么不工作?

java - 加载 JSON 文件,迭代条目并在 Java 中编辑它们

c++ - 预编译 header 和 ASLR 有什么问题?

c - 如何从文本文件中读取整数和字符并将它们存储在单独的数组中?

python - 无法从函数更改全局变量