簡體   English   中英

我將如何讀取 C 中的文本文件?

[英]How would I read a text file in C?

我有file.txt

123456 2,00 beer
234567 2,50 milk
345678 3,30 ice cream

我想將此信息放入我的動態二維數組中:

char **dataBase;
dataBase = (char**)malloc(NUM_OF_PROD * sizeof(char*));
for(i = 0; i < NUM_OF_PROD; i++){
           dataBase[i] = (char*)malloc(MAX_BUFFER* sizeof(char));
}

但我不知道怎么做。 我們這里有 3 行。 如果是 C++,我會使用getline()但在這種情況下我找不到解決方案。

在您提到的評論中,您只關心實際讀取文件。 以下是讀取文件的方法(當前未經測試,二進制模式):

#include <stdio.h>

int main()
{
    FILE *file = fopen("path/to/your/file/yourfile.txt", "rb");
    if(!file) return 1; //something went wrong!
    long size = fseek(file, 0, SEEK_END);
    char *buf = malloc(size);
    fread(&buf, size, 1, file); //read all contents, once
    fclose(file);
    free(buf); //because this is just an example
    return 0;
} 

有關閱讀文件的更多信息,只需在谷歌上進行快速搜索,您幾乎可以找到所需的一切。

您可以使用fgetcrealloc實現您自己的getline版本。

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

char *getline(FILE *file)
{
    size_t size = 16; // Size of memory allocated for line
    size_t len = 0;   // Characters read
    char *line = malloc(size);

    // Return NULL if memory allocation fails
    if (line == NULL)
        return NULL;

    for(;;) {
        int c;

        switch (c = fgetc(file)) {
            // If End Of File is met, return the line up until this point
            // if anything has been read
            case EOF:
            if (len == 0) {
                free(line);
                return NULL;
            }
            else {
                line[len+1] = '\0';
                return line;
            }

            case '\n':
            line[len+1] = '\0'; // NUL terminate the string
            return line;

            default:
            line[len++] = c;
        }

        // If the string plus NUL terminator is longer than size
        // double the size of line
        if (len + 1 >= size) {
            size *= 2;
            line = realloc(line, size);
            // Return NULL if memory allocation fails
            if (line == NULL)
                return NULL;
        }
    }
}

也可以在網上找到許多相同功能的免費/開源實現。 比如這個 GPL 2 one 如果您使用的是 POSIX 系統(例如 OS X 或 Linux),那么在stdio.h已經有一個getline版本。

我通常對文件逐行使用 fgets() 函數(前提是它是一個文本文件)。

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

#define LINELEN 200
#define NAMELEN  40

struct PRICELIST
{
  char item[NAMELEN];
  float price;
  unsigned int order_no;
  struct PRICELIST *next;
  struct PRICELIST *prev;
};

void list_print_node (struct PRICELIST *node)
{
  printf ("%d   %4.2f   %s\n", node->order_no, node->price, node->item);
}

void list_print (struct PRICELIST *head)
{
  printf ("Order #  Price  Item\n");
  printf ("------------------------------\n");
  while (head)
  {
    list_print_node (head);
    head = head->next;
  }
}

void list_delete (struct PRICELIST *head)
{
  if (head)
  {
    /* recursive call */
    list_delete (head->next);
    free (head);
  }
}

struct PRICELIST *list_read (char *filename)
{
  FILE *file;
  char line[LINELEN];
  struct PRICELIST *pricelist, *node, *prev;
  char *p;
  size_t len;

  file = fopen (filename, "r");
  if (file == NULL)
  {
    perror (filename);
    return NULL;
  }
  pricelist = NULL;
  prev = NULL;
  while (1)
  {
    if (fgets (line, sizeof(line), file) == NULL)
      break;

    /* eat the newline at the end of the buffer, be CR/CRLF agnostic .. */
    len = strlen (line) - 1;
    if (line[len] == '\r' || line[len] == '\n')
    {
      line[len] = '\0';
      len --;
    }
    if (line[len] == '\r' || line[len] == '\n')
      line[len] = '\0';

    /* allocate a new node in the list */
    node = malloc (sizeof (struct PRICELIST));
    if (node)
    {
      /* now use sscanf() for getting single elements */
      sscanf (line, "%d %f", &node->order_no, &node->price);

      /* since the item name might contain spaces this is not so easy .. */
      p = line;
      while (isspace(*p)) p++;
      while (isdigit(*p)) p++;
      while (isspace(*p)) p++;
      while (isdigit(*p)) p++;
      while (ispunct(*p)) p++;
      while (isdigit(*p)) p++;
      while (isspace(*p)) p++;
      strncpy (node->item, p, sizeof(node->item));
      node->next = NULL;

      /* if this is the first node of the list assign the head to it */
      if (pricelist == NULL)
        pricelist = node;

      /* append the new node to the end of the linked list */
      if (prev)
        prev->next = node;
      node->prev = prev;

      /* save it for the next entry */
      prev = node;
    }
  }
  /* we are done with the file, close it */
  fclose (file);
  return pricelist;
}

/* let's test it */
int main (int argc, char *argv[])
{
  struct PRICELIST *pricelist;

  if (argc < 2)
  {
    printf ("Usage: %s filename\n", argv[0]);
    return 0;
  }
  pricelist = list_read (argv[1]);
  if (pricelist)
  {
    /* print the list */
    printf ("This is the price list (filename '%s'):\n\n", argv[1]);
    list_print (pricelist);

    /* delete the list */
    list_delete (pricelist);
  }
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM