简体   繁体   English

将文本文件中的行读取到字符串指针数组中。

[英]Reading lines from a text file into a pointer array of strings.

I've been all over Stack Exchange and I haven't seen any threads that answer my question, but please direct me to one if I missed it.我已经在 Stack Exchange 到处都是,我还没有看到任何回答我的问题的线程,但如果我错过了,请指导我找到一个。

I'm using C.我正在使用 C。

So I've got a text file where the first nine lines are labels (with spaces), and each line after that is nine pieces of data that correspond to those labels.所以我有一个文本文件,其中前九行是标签(带空格),之后的每一行是与这些标签对应的九个数据。 I've declared a pointer array:我已经声明了一个指针数组:

char * cp_labels[9];

And I'm trying to read each of the first 9 lines from the text file into each element of the array.我正在尝试将文本文件中的前 9 行中的每一行读入数组的每个元素中。

The text file looks like this:文本文件如下所示:

Et jet 1
Et jet 2
Et jet 3
Eta 1
Eta 2
Eta 3
Met
Ht
Njet
(double) (double) (double) (double) (double) (double) (double) (double) (double)
(double) (double) (double) (double) (double) (double) (double) (double) (double)
(double) (double) (double) (double) (double) (double) (double) (double) (double)
...

And so on.等等。 Right now I'm just trying to make sure the strings are storing in the array correctly.现在我只是想确保字符串正确存储在数组中。 I'm working with this:我正在处理这个:

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

int main ()
{
  FILE *fp_data;
  fp_data = fopen("data.dat", "r");
  char *cp_labels[9];

  for (int l=0;l<9;l++)
    {
      fscanf(fp_data, "%s", cp_labels[l]);

      printf("%s\n", cp_labels[1]);
    }

  return 0;
}

The problem I'm running into is that我遇到的问题是

fscanf

is treating each string as a string (as it should) and I'm trying to get it to treat each line as a string.将每个字符串视为一个字符串(应该如此),我试图让它将每一行视为一个字符串。

fgets 

is an option I'm aware of, but I can't seem to get it to work with a pointer array of strings.是我知道的一个选项,但我似乎无法让它与字符串指针数组一起使用。 Any ideas?有任何想法吗? I'm doing it this way because I want to be able to use the elements of this array as labels later.我这样做是因为我希望以后能够使用这个数组的元素作为标签。

This code does the job:这段代码完成了这项工作:

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

int main(void)
{
    const char filename[] = "data.dat";
    FILE *fp = fopen(filename, "r");
    if (fp == 0)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", filename);
        return 1;
    }

    char *cp_labels[9];
    char buffer[4096];

    for (int i = 0; i < 9 && fgets(buffer, sizeof(buffer), fp) != 0; i++)
    {
        buffer[strcspn(buffer, "\n")] = '\0';
        cp_labels[i] = strdup(buffer);
    }

    for (int i = 0; i < 9; i++)
        printf("%d: [%s]\n", i, cp_labels[i]);

    for (int i = 0; i < 9; i++)
        free(cp_labels[i]);

    fclose(fp);
    return 0;
}

Sample output (from your data file):示例输出(来自您的数据文件):

0: [Et jet 1]
1: [Et jet 2]
2: [Et jet 3]
3: [Eta 1]
4: [Eta 2]
5: [Eta 3]
6: [Met]
7: [Ht]
8: [Njet]

This uses fgets() because it is easy to use and get right.这使用fgets()因为它易于使用且正确。 Using getline() is equally easy:使用getline()同样简单:

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

int main(void)
{
    const char filename[] = "data.dat";
    FILE *fp = fopen(filename, "r");
    if (fp == 0)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", filename);
        return 1;
    }

    char *cp_labels[9];
    char *buffer = 0;
    size_t buflen = 0;

    for (int i = 0; i < 9 && getline(&buffer, &buflen, fp) != -1; i++)
    {
        buffer[strcspn(buffer, "\n")] = '\0';
        cp_labels[i] = buffer;
        buffer = 0;
        buflen = 0;
    }
    free(buffer);

    for (int i = 0; i < 9; i++)
        printf("%d: [%s]\n", i, cp_labels[i]);

    for (int i = 0; i < 9; i++)
        free(cp_labels[i]);

    fclose(fp);
    return 0;
}

That produces the same output.这会产生相同的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM