954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

copying(modifying) a file's contents

I am writing a code to open a command line, prompt for the name of a file to copy, prompt for a new file name, then it should copy the file (adding a header and line numbers plus adding .lis to the filename of the new file). I BELIEVE I have the code correct for the header and line numbers, but I am getting a runtime error. I am getting an error of: "Access violation writing location 0x00000000." Can anyone help me understand what I have done wrong in my code. I simply want it to be able to copy the file for now. Once it will copy a file, I can look at the new file to see what needs debugged in regards to the header and line numbers.

I appreciate any help you can give.

/* Program to take a command line argument that is the name of a text file and creat a new text file
 * with a heading line and the contents of the original file with line numbers added.  New file
 * appends ".lis" to the new filename prior to the extension.
 */

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

 int
 main(int argc, char *argv[])
 {
    FILE *inp, *outp,;
    char ch;
    int i = 0;

    printf("Enter the name of the file you want to back up.>");
    scanf("%s", argv[0]);
    printf("Enter the name of the file to back up to.>");
    scanf("%s", argv[1]);

    if (argc == 2)
    {
        inp = fopen(argv[0], "r");
        if (inp == NULL)
        {
            printf("\nCannot open file %s for input\n", argv[0]);
        }

        outp = fopen(argv[1], "a+");
        if (outp == NULL)
        {
            printf("\nCannot open file %s for output\n", argv[1]);
        }

        fprintf(outp, "%s%s%s%s", "*****************", argv[1], ".lis", "*****************\n");


        for (ch = getc(inp); ch != EOF; ch = getc(inp))
            {if (ch == '\n')
                {
                    fprintf(outp, "\n%d", i);
                    ++i;
                }
            }
            putc(ch, outp);

        fclose(inp);
        fclose(outp);
        printf("\nCopied %s to %s.lis\n", argv[0], argv[1]);

    }
    else
        printf("Invalid argument list:  Include two file names\n");

    return(0);
 }
Griff0527
Junior Poster in Training
55 posts since Apr 2010
Reputation Points: 13
Solved Threads: 0
 

Why do you have these lines if your using command line agruements
to pass the values?

printf("Enter the name of the file you want to back up.>");
    scanf("%s", argv[0]);
    printf("Enter the name of the file to back up to.>");
    scanf("%s", argv[1]);
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

In addition, you cannot read into argv. That's undoubtedly where it's going wrong.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Thank you guys. I got it sorted out.
Much appreciated.

Griff0527
Junior Poster in Training
55 posts since Apr 2010
Reputation Points: 13
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: