简体   繁体   English

如何使用字符串数组使用用户定义的 function 从文件中读取多行

[英]How to use string array to read multiple lines from files using an user defined function

I am new to the concept of using user defined functions in file handling.我不熟悉在文件处理中使用用户定义函数的概念。 Following are the actions I would like to perform:以下是我想要执行的操作:

  1. Open file打开文件
  2. Use fgets() function to read a line of code and store it in the string array使用 fgets() function 读取一行代码并存入字符串数组
  3. Same way store each line as 1 string array element.同样的方式将每一行存储为 1 个字符串数组元素。

Using fgets() to store each line from file to string array without user defined function:使用fgets()将每一行从文件存储到字符串数组,无需用户定义 function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    FILE* fptr1;

    char words[43][40];
    fptr1 = fopen("Skills.txt", "r");
    if (fptr1 == NULL)
    {
        printf("file doesnot exist\n");
        exit(1);
    }
    while (!feof(fptr1))
    {
        fgets(words[i], 40, fptr1);
        i++;
        count++;
    }

That code worked well normally, but when I tried to use a user defined function to do the job, I got an error.该代码运行正常,但是当我尝试使用用户定义的 function 来完成这项工作时,出现了错误。

Using fgets to store each line from file to string array with user defined function:使用 fgets 将文件中的每一行存储到用户定义的 function 的字符串数组中:

void getSkillsKeywords(char *WORDS[], int* COUNT, FILE* filename);
int main()
{
    FILE* fptr1;
    char *words[43];
    int count = 0;
    fptr1 = fopen("Skills.txt", "r");
    if (fptr1 == NULL)
    {
        printf("file doesnot exist\n");
        exit(1);
    }
    getSkillsKeywords(words, &count, fptr1);

   for(int i=0;i<count;i++)
        printf("%s\n", words[i]);
    fclose(fptr1);
}
void getSkillsKeywords(char *WORDS[], int *COUNT, FILE* filename)
{
    int i = 0;
    char *WORDSS[43];
    while (!feof(filename))
    {
        fgets(WORDS[i], 40, filename); //exception is thrown saying Access violation while reading from file
        i++;
        *COUNT++;
    }
}

I have also tried to make another user defined function which is as follows:我还尝试制作另一个用户定义的 function,如下所示:

   int main()
   {
       FILE* fptr1;
       int count=0;
       fptr1 = fopen("Skills.txt", "r");
       if (fptr1 == NULL)
       {
           printf("file doesnot exist\n");
           exit(1);
       }
       char** words=getSkillsKeywords(&count,fptr1);
       for(int i=0;i<count;i++)
           printf("%s\n", words[i]);
       fclose(fptr1);
    }

    char** getSkillsKeywords(int* COUNT, FILE* filename)
    {
      int i = 0, index, u;
      char** WORDS =malloc(90*sizeof(char*));
          while (!feof(filename))
          {
              printf("came in");
              fgets(WORDS[i], 40, filename); // same exception thrown
              i++;
              *COUNT++;
          }
      return WORDS;
   }

Since I use array of strings I was not able to use any other function than fgets() where I need to store each line as an element in array.由于我使用字符串数组,因此我无法使用任何其他 function 而不是fgets()我需要将每行作为元素存储在数组中。 Where is the mistake, and how do I fix it?错误在哪里,我该如何解决?

Just fill the words array that was declared in your main function.只需填写在您的主 function 中声明的单词数组。

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

void getSkillsKeywords(char [][40], int *, FILE *);

int main() {
  FILE* fptr1;
  char words[43][40];
  int count = 0;
  fptr1 = fopen("Skills.txt", "r");

  if(fptr1 == NULL) {
    printf("file does not exist\n");
    exit(1);
  }

  getSkillsKeywords(words, &count, fptr1);
  fclose(fptr1);

  for(int i = 0; i < count; i++)
    printf("%s", words[i]);

  return 0;
}

void getSkillsKeywords(char WORDS[][40], int *COUNT, FILE* filename) {
  // no need for the vriable `i` since you have the variable `COUNT`
  while(!feof(filename))
    fgets(WORDS[(*COUNT)++], 40, filename);
}

example Skills file示例技能文件

hello Hi there
Wonderful world
Grand master
How is it going
Apples and oranges

the output is the same note that I have removed the newline from printf function because the strings already have new line at the end. output 与我已从 printf function 中删除换行符的注释相同,因为字符串末尾已有新行。

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

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