Hello…
I am currently working on a problem that requires a program that takes a command line argument that is the name of a text file and creates a new text file with a heading line and the contents of the original file with line numbers added. If the file’s name contains a period, use the part of the name before the period concatenated with .lis as the name of the new file. Otherwise, just concatenate .lis with the while file name.
That is the assignment as copied from the textbook. There is a sample program that is pretty close to the core of this problem, so I copied it out of the book as it was. I was going to test it then start the modification to give the output required. But, I have run into a snag. When I copy the program out of the text, I get an assertion failure. I have checked and rechecked the code to make sure I have everything exact, and I do. It compiles just fine, but at runtime I get the failure. Can anyone give me a clue as to why this isn’t working?
I am using Visual Studio 2008. This is the exact error:
Debug Assertion Failed!
Program:…Assignment_7-1.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\fopen. c
Line: 54
Expression: (file != NULL)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
This is my code that caused that failure:
Code:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc,
char *argv[])
{
FILE *inp,
*outp;
char ch;
//char name[30];
//printf("\nType backup and the name of the file to copy and the new file name >");
inp = fopen(argv[1], "r");
if (inp == NULL) {
printf("\nCannot open file %s for input\n", argv[1]);
exit(1);
}
outp = fopen(argv[2], "w");
if (outp == NULL) {
printf("\nCannot open file %s for output\n", argv[2]);
exit(1);
}
//strcat(argv[2], ".lis");
// fprintf(output, "***************** %s ***************", argv[2]);
/* char line[500];
int counter = 1; */
/*fgets(line,500,input);*/
/* while(!EOF (input)) {
fgets(line,500,input);
fprintf(output, "%d : %s", counter, line);
counter++;
}*/
for (ch = getc(inp); ch != EOF; ch = getc(inp))
putc(ch, outp);
fclose(inp);
fclose(outp);
printf("\nCopied %s to %s\n", argv[1], argv[2]);
return(0);
}
This code is directly out of the book, but when I test it before moving on with my modification to complete the rest of the assignment, it fails.
Thanks for taking a look and offering any advice.