【问题标题】:C programming. File I/O, Link List and StructC 编程。文件 I/O、链接列表和结构
【发布时间】:2016-08-16 15:06:28
【问题描述】:

我有一个任务要求我编辑一个提供给我的 C 程序,以便它可以从文本文档中读取,其中每一行如下所示:

int%char%char%Double%int%int%int

在文件末尾有任意数量的行和一个空行。

这个文本文件被传递给这个程序:

#include <stdlib.h> 
#include <stdio.h> 
struct node{  
  int element; 
  struct node * next;
}; 
// node structure 
struct node * head = NULL; // head node
void add_node(int num) {
  if (head == NULL){
    head = (struct node *) malloc(sizeof(struct node));
    head->element = num;
  }
  else{
    struct node * p = head;
    while(p->next != NULL)
    p = p->next;
    p->next = (struct node *) malloc(sizeof(struct node));
    p->next->element = num;
  }
 }
void print_list() {
  struct node * p = head;
  while(p != NULL){
    printf("%d ", p->element);
    p = p->next;
  }
 } 
void free_list() {
  struct node * p = head;
  while(head != NULL){
    p = head->next;
    free(head);
    head = p;
  }
 }
int main(int argc, char const *argv[]) {
  int n, i;
  for (i = 0; i < 10; i++) {
    scanf("%d", &n);
    add_node(n);
  }
  print_list();
  free_list();
  return 0;
 }

我需要编辑此程序以在结构节点中包含文件中的 7 个字段(ID、类别、详细信息、金额、年、月、日)。然后让它从文本文件中读取(现在是 File_name.txt)在结构节点中添加它们之间没有 % 分隔符的字段,然后按顺序打印出来(RecordID:(ID)类别:(类别)金额: $(amount) Date: (Month)-(Day)-(Year) Detail: (detail)) 并在程序终止前释放所有指针。我不希望你们都为我做任务,只是我不知道 C 编程是如何工作的,我需要这样做,所以如果有人能帮助我指出正确的方向如何去做这件事不胜感激。

【问题讨论】:

  • 听说过fscanf? scanf 系列函数将为您提供极大的帮助
  • o_0 如果你不会 C 语言,那是一件很麻烦的事情。你知道指针在另一种语言中是如何工作的吗?
  • 不,我不知道指针是如何工作的:/ 我想我可以弄清楚主要的,我从老师那里得到了如何编辑结构节点但我不知道如何添加使用 add_node 从文本文件到结构的值。
  • 你应该开始学习如何使用fopen、fgets,尤其是fscanf()来打开和读取文件......你知道数据结构吗?你知道什么是链表吗?然而,问一个新手程序员很奇怪....
  • 顺便说一句 head-&gt;element = num; --> head-&gt;element = num;head-&gt;next = NULL;, p-&gt;next-&gt;element = num; 同上。

标签: c parsing struct io


【解决方案1】:

这应该会让你走上正确的道路:

int main(int argc, char const *argv[]) {
  char filename[100];

  printf("Enter the name of the file: ");

  /*stdin is a FILE pointer to standard input */
  fgets(filename, 100, stdin);

  /*Now you open the file for reading "r" means read mode*/
  FILE *fp = fopen(filename, "r");

  /*Basic error checking*/
  if (!fp) {
    perror("File opening failed");
    return EXIT_FAILURE; /*This is macro defined in stdlib.h*/
  }

  int id, year, month, day;
  char category, detail;
  double amt;

  /* Here the fun begins. Use fscanf*/
  while(fscanf(fp, "ID(%d)%%Category(%c)%%Detail(%c)%%Amount(%lf)%%Year(%d)%%Month(%d)%%Day(%d)",
    &id, &category, &detail, &amt, &year, &month, &day) == 7) {

    /* Do what ever you want with those values - create a node or something*/
  }

  /* Finally close the file */
  fclose(fp);
  return 0;
}

请记住,每次通过循环,变量都会发生变化。

【讨论】:

  • 谢谢,这将非常有帮助,虽然该文件不是基于用户输入,但我至少知道如何阅读它。再次感谢您。
【解决方案2】:

据我所见。

  • 您必须使用文件中的指定变量创建自己的结构。

  • 读取每一行并智能解析它们......函数 strtok 应该能够在 C 中做到这一点。

  • 提取变量以存储在创建的结构中,使用一个写起来应该不会太难的函数

  • 其余的你可以或应该能够照顾

这可能没有您希望的那么有用,但是您可以尝试让我理解,以便我可以帮助做得更好

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多