Thread: program that reads several files & writes concatenated contents into file

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    program that reads several files & writes concatenated contents into file

    I'm not really sure what this is asking me to do. Please help!
    DUE at 9pm and I have no clue what to do.
    I'll keep working on what I have though..
    (see at end of post)


    In this quiz you will write a program (quiz4.c) that sequentially
    reads several files and writes their concatenated contents into
    another file. The names of the files to be read and the file to
    write to will be specified on the command line. For example, after
    you compile your code and run:

    iacs5.ucsd.edu% ./a.out quiz3.c quiz4.c output

    the file output should contain the concatenated contents of both
    quiz3.c and quiz4.c. The last parameter is always the name of the
    file where you write to (destination file) whereas all other
    parameters are the names of the files you read and concatenate in
    the order they appear. Your program should perform the following
    checks:

    a) If less than two parameters are passed to the program, abort with
    an error message (at a minimum there should be one file to read
    and the destination file);

    b) If a you cannot open the destination file for writing, abort with
    an error message;

    c) If you cannot open any of the files to be read issue a warning
    message and continue to the next file. If this is the last file
    then simply close the destination file and exit your program
    without further messages.

    You must use fgets in order to read files and fprintf to write to
    the destination file. Take a look at program w6-5.c to see how fgets
    works and program w5-8 to see how to retrieve command line
    arguments.

    HINT: You can use the unix commands cat and diff to check your
    program. If you run

    iacs5.ucsd.edu% ./a.out quiz3.c quiz4.c output.1
    iacs5.ucsd.edu% cat quiz3.c quiz4.c > output.2

    then the command

    iacs5.ucsd.edu% diff output.1 output.2

    should produce no output if you have done your job right. Otherwise
    you will see a number of complaints about differences in the two
    files.


    quiz3.c & quiz4.c are just examples. In practice, any files you input should work (as long as the files exist).

    Here's what I have so far:
    I don't know if i'm anywhere close to the right answer..

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define FILEMAX 1000000
    
    FILE *fp;
    
    main ()
    {
    	
    for(i=1; i<3; i++)
    {
    printf("Input filename:");
    fscanf("%s",filename);
    fp=fopen(filename, "a");
    	if(fp==NULL)
    	{
    		printf("ERROR\n");
    	}
    		exit(1);
    }

    Thank you!!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    do you know anything about argc and argv[ ] ? you MUST use them...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I know about them but I don't fully know how to use them.
    I believe I have to use "cat" as well....this would help me combine the files, right?

    Here's what I have now:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define FILEMAX 1000000
    
    FILE *fp;
    
    int main (int argc, char *argv[])
    {
    char data[FILEMAX];
    char filename[………..};
    ………………………………
    
    if(argc== …………….
    
    	
    for(i=1; i<3; i++)
    {
    printf("Input filename:");
    fscanf("%s",filename);
    fp=fopen(filename, "a");
    	if(fp==NULL)
    	{
    		printf("ERROR\n");
    	}
    		exit(1);
    }

    Sorry about the "..............."'s
    I placed those where I didn't know what to write..

    Thanks so much for helping!
    Please let me know what I should do next..
    Last edited by sam...; 11-17-2010 at 07:55 PM.

  4. #4

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    look...

    argc is the number of arguments you give after a.out. so, if you type a.out 2 4 56 5 argc will be 5 got it? now, argv is something like an array...imagine that in the first cell you "a.out" in the second the number 2 in the third the numer 4 etc...does this helps a little?

  6. #6
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    more from FAQ:
    Cprogramming.com FAQ > Accessing command line parameters/arguments

    Don't know if this helps or confuses things, but here's some public domain stuff I wrote back in the day to print out all the arguments (backwards) including the program name (argument 0).
    Code:
    #include <stdio.h>
    /* Print command line arguments and exit. */
    int main (int c, char **v){
        while (c--){
            printf ("Argument %i is \"%s\"\n",c,v[c]);
        }
        return 0;
    }
    Last edited by hellork; 11-17-2010 at 08:11 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    It is definitely starting to make more sense and the examples help.
    Thank you both so much!!

    I think I understand argc but I'm still a bit confused about argv.
    Also, how would I get the program to prompt the user for file names?
    I think this involves fgets and fscanf (and fprintf for the final file) but I'm not completely sure how to go about writing this.

    I do remember reading something about printing arguments backwards but I don't know how I would use it for this particular program.

    Thank you again!

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    when working with files you definitely use fscanf, fprintf etc...actually this functions are made for this reason...

    Now let me show you an example about argv[ ]...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
    	printf("argc: %d\n", argc);
    	printf("argv[0]: %s\n", argv[0]); 
    	printf("argv[1]: %s\n", argv[1]); 
    	printf("argv[2]: %s\n", argv[2]); 
    return 0;
    }

  9. #9
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39

    args

    Quote Originally Posted by sam... View Post
    It is definitely starting to make more sense and the examples help.
    Thank you both so much!!

    I think I understand argc but I'm still a bit confused about argv.
    Also, how would I get the program to prompt the user for file names?
    I think this involves fgets and fscanf (and fprintf for the final file) but I'm not completely sure how to go about writing this.

    I do remember reading something about printing arguments backwards but I don't know how I would use it for this particular program.

    Thank you again!
    Are you reading different assignment instructions than what you posted, sam? This one said to use command line arguments, not prompt for file names.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    a) If less than two parameters are passed to the program, abort with
    an error message (at a minimum there should be one file to read
    and the destination file);
    Code:
    /* your program name will be 1, so you need two more files, for 3, minimum */
    if argc < 3  
      print"Two file minimum is required - terminating\n"
      exit(1)
    end if
    b) If a you cannot open the destination file for writing, abort with
    an error message;
    Code:
    char filename[80];
    FILE *fp;
    
    fp equals fopen(argv[argc], "a")
    if fp == NULL
      print "Error opening output file - terminating\n"
      exit(1)
    end if
    c) If you cannot open any of the files to be read issue a warning
    message and continue to the next file. If this is the last file
    then simply close the destination file and exit your program
    without further messages.
    Code:
    for i equals 1; i less than argc; i++
      fp equals fopen(argv[i], "r")
    
      if fp is NULL && i less than argc - 2  //
        print "Warning: An input file failed to open - continuing
      end if
    etc.

    Just tick them off, one after the other. Meter by meter, life is sweeter.

    Your code should have no "Enter filename" stuff, in it. That is being handled via argc and *argv[], for you.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by hellork View Post
    Are you reading different assignment instructions than what you posted, sam? This one said to use command line arguments, not prompt for file names.
    Sorry about that. I was confused because I didn't really understand argc and argv at the time.
    I think it makes more sense now.
    I'll work on it some more. Thank you!

    Adak, thank you for your great help as well.
    I have a question about this part of the code though:

    Code:
    char filename[80];
    FILE *fp;
    
    fp equals fopen(argv[argc], "a")
    if fp == NULL
      print "Error opening output file - terminating\n"
      exit(1)
    end if
    Why does it have "80"?
    Why not some other number? Is there a reason?

    Also, in addition to the parts a) through c) that you have given me, would I keep the same header or are parts of it unnecessary? Examples I looked at only used stdio.h. Would that be enough?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main (int argc, char *argv[])
    {
    Lastly, do I need to include something like
    Code:
    #define FILEMAX 1000000
    in the beginning of the code?

    Thank you so much!
    Last edited by sam...; 11-17-2010 at 09:15 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I was wondering...
    You haven't used "fprintf" or "fgets". Will it still work?






    Quote Originally Posted by Adak View Post
    Code:
    /* your program name will be 1, so you need two more files, for 3, minimum */
    if argc < 3  
      print"Two file minimum is required - terminating\n"
      exit(1)
    end if

    Code:
    char filename[80];
    FILE *fp;
    
    fp equals fopen(argv[argc], "a")
    if fp == NULL
      print "Error opening output file - terminating\n"
      exit(1)
    end if

    Code:
    for i equals 1; i less than argc; i++
      fp equals fopen(argv[i], "r")
    
      if fp is NULL && i less than argc - 2  //
        print "Warning: An input file failed to open - continuing
      end if
    etc.

    Just tick them off, one after the other. Meter by meter, life is sweeter.

    Your code should have no "Enter filename" stuff, in it. That is being handled via argc and *argv[], for you.

  13. #13
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Here is a similar snippet, written in Anchor script. Anchor is C (but punctuation is sort of optional).
    Stuff like this actually compiles with it
    Anchor | freshmeat.net

    Error checking and usage information would go after the closing brackets, of course...

    Code:
        /* argc is always 1 more than what we need... */
        argc--
        /* is the number of files correct? */
        if  argc > 2
            /* yes, can we open last file for append in binary mode? */
            if  (f_out=fopen(argv[argc],"ab+"))
                /* yes, let's iterate through each file */
                for  this_file=1; this_file < argc; this_file++
                    /* can the file can be opened for reading? */
                    if  (f_in=fopen(argv[this_file],"rb"))
                        /* yes, read all of f_in and append to f_out */
                        while  fgets(buf,buf_MAX,f_in)
                            fprintf  f_out,"%s",buf
        return 0

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    another example...i believe it compiles...
    Code:
    #include <stdio.h>
    
    int main(void){
    
         FILE *infile, *outfile;
    
         int c;
    
         char buf[10];
    
         
    
         if((infile=fopen("lala.txt", "r"))==NULL){
    
              exit(1);
    
         }   
    
         while(fgets(buf, 10, infile) != NULL){
    
              fputs(buf, outfile);
    
         }
    
         fclose(infile);
    
         fclose(outfile);
    
    return 0;
    
    }

  15. #15
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Hmm. Actually the directions say at minimum 1 input and 1 output file, so my > 2 was too much. Oh well. Not going to be a perfectionist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM