简体   繁体   中英

Reading in multiple data files from a mapping file in C

First off I am creating a program that will read lines of characters and find words (they don't have to have meaning, ie 'ab' could be word ) and storing them in the appropriate data structure. I used trie structure to store the words. I am given a mapping file as a command line argument yet inside the mapping file I have two data files I need to gain information from. The usage interface is as follows: first(program name) <mappingfile> .

Inside the mapping file, there exists two data files: <dictFile> and <dataFile> . Im not sure how to read and store the information presented the two data files. So far I have the following:

#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;
  }
}

My goal is to have pointers point to respective data files in the mapping file which I can use for reading their contents. I will be given the following input in the command line: first(program name) <mappingfile> .

Inisde the mapping file contains the lines of two plain .txt files in the form <dictFile> <dataFile> .

I wish to access both contents of <dictFile> and <dataFile> .. with pointers to the respective file.

If I understand you correctly this should do it. Note that it assumes your filenames don't have any spaces. And if you want to use the "non secure" api's you need to add _CRT_SECURE_NO_WARNINGS to the project properties under Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions.

#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;
}

If I understand your question correctly you want to parse a file where each line contains the filenames of two other files and then read from these. What you can do is use fgets to read your mapping file line by line. What you can do next is use the function strtok to split your string on a whitespace. I'll break it down for you step by step.

Firstly we want to open the mapping file for reading

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

This will try to open the mapping file specified by the command line arguments of your program and if it fails it will print a corresponding error message.

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

After we've opened the file we want to iterate through all the lines until we reach the end of the file and fgets will return NULL. fgets will put the current line into buf .

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

We need to split the line read by fgets by a delimter (a whitespace) so we know which part corresponds to the dictfile and the datafile. This is done by using the strtok function which returns a pointer to the substring before the whitespace and when passing in NULL it will return a pointer to the substring after the whitespace. A slightly weird way of removing any trailing newlines is to use strtok and the newline as a delimiter.

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;
}

Very similiarly to how we open the mapping file, we now open the two files found on the current line read by fgets with "r" mode which opens for reading. If the file does not exist or cannot be found, the fopen call fails.

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);

This is a very simple method of "dumping" the content of the files. It uses getc to read the next char from the file and prints it until it reads EOF. This is where you should do your own function.

fclose(dictfile);
fclose(datafile);

And don't forget to close the files afterwards or you will leak resources.

Finally here is the code on what I just described

#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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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