Member Avatar for Griff0527

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

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

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

Member Avatar for Griff0527

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.