File names as function arguments

This is a discussion on File names as function arguments within the C Programming forums, part of the General Programming Boards category; In main() I want to declare the two files I'll be working with as strings, char*source and char*dest. Eventually, I'll ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    2

    Question File names as function arguments

    In main() I want to declare the two files I'll be working with as strings, char*source and char*dest. Eventually, I'll be reading one line at a time from the source and sending that string to a strtok function to get parsed and written to a structure, which I'll then write to my dest file. Before I do any of that though, I need to correct however I am incorrectly handling the files.
    I apologize for the incompleteness of this, the primary thing I'm trying to do and test for. Is to send a string from main as the file name to the function, and have the function open the files by using the strings as arguments to fopen. Any help would be greatly appriciated.
    Here's where I'm stuck at:
    Code:
    int main (int argc, char *argv[]){
        char *source = "ascii.txt";
        char *dest = "binary.bin";
        
        int num_read = 0;
        int num_skipped = 0;
        
        ZIPS_create_bin_from_ascii(source, dest, &num_read, &num_skipped);
        
        printf("num_read = %d\n", num_read); //test, show number of lines in file
        
        return 0;
    }
    int ZIPS_create_bin_from_ascii(char *ascii_file, char *binary_file,
                                   int *recs_read, int *recs_skipped){
        FILE *sfp;
        FILE *dfp;
        char str[512];
        
        sfp = fopen(ascii_file, "rb");
        dfp = fopen(binary_file, "wb");
        
        if(sfp || dfp == NULL)
            exit(1);
        
        else{
            while(fgets(str, 511, sfp) != NULL){   //going wrong here
                    ++recs_read;
            }
            fclose(dfp);
        }
        fclose(sfp);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Code:
    if(sfp || dfp == NULL)
    This is wrong. You probably meant:
    Code:
    if(sfp == NULL || dfp == NULL)
    Code:
    while(fgets(str, 511, sfp) != NULL){
    Why did you subtract a byte from the buffer? If the buffer is 512 bytes, then pass 512 to fgets(). You can even pass in sizeof(str) instead of hard coding the buffer size.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    2

    Lightbulb

    It was in my function prototype. I was trying to pass a character string to a function that opens and reads the file. I didn't realize to do that, I needed to pass a FILE *pointer. Here it is, a bit more complete.

    In my main, I'm initializing the strings and passing by reference to my function.
    Code:
    int main (void){
        char *source = "zips_short.txt";
        char *dest = "binary.bin";
        
        int n_read = 0;
        int n_skip = 0;
    
        ZIPS_create_bin_from_ascii(source, dest, &n_read, &n_skip);
        
        printf("n_read = %d\n", n_read);
        printf("n_skip = %d\n", n_skip);
        
        system("pause");
        return 0;
    }
    Purpose of the function is to read a text file containing record data, line by line, and send them to a parse function to be validated and written to a structure.

    Code:
    int ZIPS_create_bin_from_ascii(char *ascii_file, char *binary_file,
                                               int *recs_read, int *recs_skipped){
         FILE *sfp = NULL;
         FILE *dfp = NULL;
         
         char line[512];
         ZIPS_data_t rec;
         
         sfp = open_source(ascii_file);
         dfp = open_dest(binary_file);
         
         while(fgets(line,512,sfp)!=NULL){
            line[strlen(line)-1] = '\000';
            memset(&rec, 0, sizeof(rec));
            
            if(ZIPS_parse_zips_rec(line, &rec)){
                write_rec(dfp, rec);
                ++(*recs_read);
            }
            else
                ++(*recs_skipped);
         }
         fclose(sfp);
         fclose(dfp);
    }
    These functions support the last one.

    Code:
    FILE* open_source(char *sfp){
          FILE *file = fopen(sfp, "r");
          if(file == NULL){
                  printf("Could not open source file: %s\n", sfp);
                  exit (1);
          }
          return file;
    }
    Code:
    FILE* open_dest(char *dfp){
          FILE *file = fopen(dfp, "wb");
          if(file == NULL){
                  printf("Could not open destination file: %s\n", dfp);
                  exit (1);
          }
          return file;
    }
    Code:
    int write_rec(FILE *fp, ZIPS_data_t frec)
        fwrite(&frec, sizeof(frec), 1, fp);
    I know I should likely combine the open functions, and prompt the user for the file names and modes, but what else in here reaks of poor practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21