【问题标题】:Loading a text file into a struct using C使用 C 将文本文件加载到结构中
【发布时间】:2013-02-26 23:04:21
【问题描述】:

我尝试将文本文件的内容加载到结构中。

我的想法是这样的:

我有两个文件,struct.hmain.c 和一个 list.txt 文件。

在文件struct.h

struct analg {
    char word[6];
    char signature[6];
};
struct analg h[106];
FILE *fp;

在文件main.c

#include<stdio.h>
#include "struct.h"

void load() {
    fp = fopen("list.txt", "r");
    if(fp == NULL) {
        printf("fail");
        return 1;
    }
    else {
        printf("file loaded!\n");
    }
    fclose(fp);
    return;
}

void print() {

    int i;
    for(i=0; i<1000; i++) {
        while(fgets(h[i].word, 6, fp)) {
            printf("%s", h[i].word);
        }
    }
    return;
 }

int main () {

int choice;

do {
    printf("choose L or P: ");
    scanf("%s", &choice);
    switch(choice) {
        case 'l': 
            load();
            printf("\n[l]oad - [p]rint\n");
            break;
        case 'p': 
            print();
            printf("\n[l]oad - [p]rint\n");
            break;
        default:        
            break;
       }
    }  while(choice!='q');

    return;
}

在文件list.txt

throw

timer

tones

tower

trace

trade

tread

所以我尝试通过按“L”来加载文本文件到结构,然后当我按“p”时会显示,但它不是!

【问题讨论】:

  • scanf("%s", &amp;choice); 需要一个指向 char 数组的指针。此外,大小写很重要,您只测试小写字母。
  • 是的,当我写 p 时没有任何效果!只有L!
  • @HaidoManeen 是的,正如 DanielFischer 所说,那是因为你的代码被破坏了。
  • 坏了?我不明白?

标签: c printing struct io load


【解决方案1】:

在您的代码中,我发现有 2 个潜在问题。选择必须是基于lp 切换的字符。您可能还需要添加大小写来处理大写。

另一个问题是在load 函数中,您正在关闭文件指针。因此,当您输入print 函数时,fgets 可能无法工作,因为fp 已经关闭。

要将文件加载到结构中,必须将加载修改为

void load() {
fp = fopen("list.txt", "r");
if(fp == NULL) {
    printf("fail");
    return; // There was an error in original code as this was returning 1
}

do{
    fgets(h[count++].word, 6, fp); // Count is a global variable - no. of elements read
}while(!feof(fp));
printf("file loaded!\n");
fclose(fp);

return;
}

对应的打印函数会变成

void print(){

  int i;
  printf("Inside print\n");

  for(i=0; i < count; i++) {
      printf("%s", h[i].word);
  }
 return;
 }

主要功能是,

int main (){

char choice;


do{
    printf("choose L or P: ");
    scanf("%c", &choice); //Only character is read and hence, %s is not required
    switch(choice){
    case 'l': 
        load();
        printf("\n[l]oad - [p]rint\n");
        break;
    case 'p': 
        print();
        printf("\n[l]oad - [p]rint\n");
        break;
    default:
    case 'q':
        break;
    }
} while(choice !='q');

return 0;
}

最后一点。在scanf 语句中,如果使用scanf("%s", &amp;choice);,则在main 退出时会生成运行时检查错误,并显示变量choice 周围的堆栈已损坏的消息。

【讨论】:

  • 好吧,你可能不明白这个问题!我想将文件加载到数组中,这就是我在 Load 函数中关闭文件的原因!我想通过结构将文件加载到数组中,然后在不更改任何内容的情况下处理文件!
  • 对不起,我也没有你当前的观点。您想在加载时关闭文件,这应该没问题。在打印中,您正在从文件指针中读取。调用 print 时文件指针不是有效句柄。
  • 所以我想,我上传文件,关闭它,让文件在结构中的数组中,打印文件! ,所以我想这样做!
  • 现在我明白了,嗯,我没有从文件中读取任何内容的问题,我尝试了你的代码,它工作得很好。非常感谢您的帮助,我很感激 - @Ganesh
【解决方案2】:

我会评论你的代码在做什么:

void load() {
fp = fopen("list.txt", "r"); // opens the file for reading
if(fp == NULL) {
    printf("fail");          // if the file couldn't be opened, return an error
    return 1;                // (aside: a void function can't return an int)
}
else {

   printf("file loaded!\n"); // tell the user that the file was opened
}

fclose(fp);                  // close the file, having read nothing from it

return;
}

您绝不会从文件中读取任何内容。因此,内存中的内容与磁盘上的内容无关。

C 没有用于序列化和反序列化结构的内置方法,因此您需要为磁盘上的文件定义正式语法并编写可以将该语法解析为结构的代码。

【讨论】:

  • 所以我阅读了txt文件但没有保存在结构中。你是这个意思吗?
  • 那么你知道我应该如何为磁盘上的文件定义一个正式的语法并编写可以将该语法解析为我的结构的代码吗?
  • 我认为你用fgets 在打印中所做的事情可能是正确的——你只是在错误的地方做这件事(至少假设你希望所有加载都是在load) 内完成,并且在您关闭文件后,您将无法再从中读取。所以在关闭文件之前,可能会在load 中执行while(fgets(h[i].word, 6, fp)),并且只在print 中执行 print[f]
  • 非常感谢,我按照你说的做了,而且有效!但是一个littleeeee问题!我只是从我的文本文件中获取第一行? ,我在加载中尝试了while(fgets(h[i].word, 6, fp)),在打印中尝试了printf("%s", h[i].word);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 2022-11-16
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多