簡體   English   中英

從C中的映射文件中讀取多個數據文件

[英]Reading in multiple data files from a mapping file in C

首先,我正在創建一個程序,它將讀取字符行並查找單詞(它們不必具有含義,即'ab'可以是單詞)並將它們存儲在適當的數據結構中。 我使用trie結構來存儲單詞。 我給了一個映射文件作為命令行參數但在映射文件中我有兩個數據文件,我需要從中獲取信息。 用法界面如下: first(program name) <mappingfile>

在映射文件中,存在兩個數據文件: <dictFile><dataFile> 我不知道如何閱讀和存儲提供兩個數據文件的信息。 到目前為止,我有以下內容:

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

void readDict(FILE *dict_file){

}

int main(int argc, char *argv[]){
  FILE* file;


  if(argc != 2){ //error in inputing, not 2 files
    printf("error\n");
    return 0;
  }

  file = fopen(argv[1],"r" ); //reading the mapping file

  input;
  if(file == NULL){ //nothing inside file
    printf("file does not exist\n");
    return 0;
  }
}

我的目標是讓指針指向映射文件中的各個數據文件,我可以使用它們來讀取它們的內容。 我將在命令行中輸入以下內容: first(program name) <mappingfile>

Inisde映射文件包含兩個普通.txt文件的行,格式為<dictFile> <dataFile>

我希望通過指向相應文件的指針訪問<dictFile><dataFile> ..的內容。

如果我理解正確的話,應該這樣做。 請注意,它假定您的文件名沒有任何空格。 如果你想使用“非安全”api,你需要在配置屬性 - > C / C ++ - >預處理器 - >預處理器定義下的項目屬性中添加_CRT_SECURE_NO_WARNINGS。

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

void readDict(FILE *dict_file){

}

int main(int argc, char *argv[]){
  FILE* file;


  if(argc != 2){ //error in inputing, not 2 files
    printf("error\n");
    return 1;
  }

  file = fopen(argv[1],"r" ); //reading the mapping file

  //input;
  if(file == NULL){ //nothing inside file
    printf("file does not exist\n");
    return 1;
  }

  char dictFileString[256], dataFileString[256];
  fscanf( file, "%255s %255s", dictFileString, dataFileString );

  FILE *dictFile, *dataFile;
  dictFile = fopen( dictFileString, "r" );
  if (dictFile == NULL) {
      printf( "%s does not exist\n", dictFileString );
      fclose(file);
      return 1;
  }
  dataFile = fopen( dataFileString, "r" );
  if (dataFile == NULL) {
      printf( "%s does not exist\n", dataFileString );
      fclose(file);
      fclose(dictFile);
      return 1;
  }

  readDict(dictFile);

  //  The additional logic would be placed here.

  fclose( dictFile );
  fclose( dataFile );

  //  If you need to read additional file names then loop
  //  back up to read the next line of 'file'

  fclose( file );
  return 0;
}

如果我正確理解你的問題,你想要解析一個文件,其中每行包含兩個其他文件的文件名 ,然后從這些文件中讀取。 你可以做的是使用fgets逐行讀取你的映射文件。 接下來你要做的是使用函數strtok將字符串拆分為空格。 我會一步一步地為你分解它。

首先,我們要打開映射文件進行讀取

if((file = fopen(argv[1],"r")) == NULL) {
  perror("error opening file");
  return 1;
}

這將嘗試打開程序的命令行參數指定的映射文件,如果失敗則會打印相應的錯誤消息。

while(fgets(buf, sizeof(buf), file) != NULL) {

在我們打開文件之后,我們想要遍歷所有行,直到我們到達文件的末尾, fgets將返回NULL。 fgets會將當前行放入buf

dictfilename = strtok(buf, " ");
datafilename = strtok(NULL, " ");
strtok(dictfilename, "\n"); /* Remove any trailing newlines */
strtok(datafilename, "\n");

我們需要將由fgets讀取的行拆分為分隔符(空格),以便我們知道哪個部分對應於dictfile和數據文件。 這是通過使用strtok函數完成的,該函數在空格之前返回指向子字符串的指針,當傳入NULL時,它將返回指向空白后的子字符串的指針。 刪除任何尾隨換行符的一種有點奇怪的方法是使用strtok和換行符作為分隔符。

if((dictfile = fopen(dictfilename,"r")) == NULL) {
  fprintf(stderr, "error opening file %s: %s\n", dictfilename, strerror(errno));
  return 1;
}

if((datafile = fopen(datafilename,"r")) == NULL) {
  fprintf(stderr, "error opening file %s: %s\n", datafilename, strerror(errno));
  return 1;
}

與我們如何打開映射文件非常相似,我們現在打開當前行上的兩個文件,這些文件由fgets讀取,其中“r”模式打開以供閱讀。 如果文件不存在或找不到,則fopen調用失敗。

printf("Content of %s:\n", dictfilename);
while ((c = getc(dictfile)) != EOF)
  putchar(c);

printf("\nContent of %s:\n", datafilename);
while ((c = getc(datafile)) != EOF)
  putchar(c);

這是一種“轉儲”文件內容的非常簡單的方法。 它使用getc從文件中讀取下一個char並打印它直到它讀取EOF。 這是你應該做自己的功能的地方。

fclose(dictfile);
fclose(datafile);

並且不要忘記之后關閉文件或者您將泄漏資源。

最后這里是我剛才描述的代碼

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

#define MAX_LENGTH 100 // change this to the actual maximum length of your lines.

int main(int argc, char **argv){
  FILE* file, *dictfile, *datafile;
  char c;
  char buf[MAX_LENGTH];
  char *dictfilename, *datafilename;

  if(argc != 2) {
    fprintf(stderr, "Usage: %s <mapping file>\n", argv[0]);
    return 0;
  }

  if((file = fopen(argv[1],"r")) == NULL) {
    perror("error opening file");
    return 1;
  }

  while(fgets(buf, sizeof(buf), file) != NULL) {
    dictfilename = strtok(buf, " ");
    datafilename = strtok(NULL, " ");
    strtok(dictfilename, "\n"); /* Remove any trailing newlines */
    strtok(datafilename, "\n");

    if((dictfile = fopen(dictfilename,"r")) == NULL) {
      fprintf(stderr, "error opening file %s: %s\n", dictfilename, strerror(errno));
      return 1;
    }

    if((datafile = fopen(datafilename,"r")) == NULL) {
      fprintf(stderr, "error opening file %s: %s\n", datafilename, strerror(errno));
      return 1;
    }

    // do something with the files (e.g read all the content)
    printf("Content of %s:\n", dictfilename);
    while ((c = getc(dictfile)) != EOF)
      putchar(c);

    printf("\nContent of %s:\n", datafilename);
    while ((c = getc(datafile)) != EOF)
      putchar(c);
    printf("\n");

    // don't forget to close the files when you're done with them.
    fclose(dictfile);
    fclose(datafile);
  }
  fclose(file);
}

暫無
暫無

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

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