Write a C program that takes a command line argument that is the name of a text file and creates a new text file with heading line *************************** file name ******************************* 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 whole file name.
Optional Information: OS: Windows XP; Browser: IE Already Tried: Request comments/remarks be documented accordingly throughout the program and request completion no later than Tues 5PM CST. Thanks.
Please try the program (code) listed below. Note that there is a similar question at http://www.justanswer.com/questions/23kme-write-c-program-takes-command but I have used different code in this example. I highly recommend that you go through the code yourself to learn what it's doing and consider playing with it to tweak it how you would like. Some notes: - it uses standard C syntax (not C++) and available C runtime libraries - to run it, assuming you save this and call it "listprog" and have a file called "file.txt" in the same directory as the program executable file: open a command prompt, change to the directory where "listprog.exe" exists, and type "listprog file.txt" (without quotes) and press enter + this will create file.lis with the line numbers - you can call it whatever you want - I've put a semicolon (":") after the line number in the fprintf - you can remove that if you want - I've not formatted the line numbers, so the first 9 will be "1" through "9" (not, for example, "000001" through "000009"); if you want to display it that way, you can change the "%d" into "%06d" (which will zero-fill the number with leading zeros and use 6 spaces/digits to display the number) - I've used a statically allocated char array of 2048 char's to read the input file lines; if you need to read files with lines longer than 2048 char's (well, 2047 once you take into account the null-terminator), this static allocation will need to be increased; it's also possible to dynamically read any size line numbers, but I have not put that into this program - If you want to use C++ style I/O (cout, ifstream, ofstream), that's easy enough to do, but the question says "C Program" which is why I went with standard C syntax. (I can rework into C++ syntax if you want, just let me know.) - If you have any problems with this code, please let me know and I'll work with you to get it corrected quickly. If you have any questions, please ask and I'll be happy to explain further. --- code below this line --- #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { FILE *fin; /* input file */ FILE *fout; /* output file */ char *filename; /* output filename */ char *strptr; /* char pointer */ char data[2048]; /* used to read line from file */ int lineno; /* line number */ int filenamelen; /* length of input/output file name */ /* command line arguments: first (index 0) is the program name, second (index 1) should be the filename */ if (argc<2) { printf("You must enter a filename on the command line when running this program.\n"); return -1; } /* create the output filename; make it big enough as the input file and ".lis" at the end */ filenamelen = strlen(argv[1]) + 4; filename = (char*)malloc(filenamelen); strcpy(filename, argv[1]); strptr = strstr(filename, "."); /* if it already has a "." in it, null-terminate it there */ if (strptr!=0) { *strptr = 0; } /* append ".lis" */ strcat(filename, ".lis"); /* open input file */ fin = fopen(argv[1], "r"); if (fin==NULL) { printf("\nUnable to open file: %s!\n", argv[1]); return -1; } /* open output file */ fout = fopen(filename, "w"); if (fout==NULL) { printf("\nUnable to open output file: %s!\n", filename); return -1; } /* header */ fprintf(fout, "*********************************%s***********************************\n", argv[1]); /* list the lines and print line numbers */ lineno = 0; while (fgets(data, sizeof(data), fin)) { fprintf(fout, "%d: %s", ++lineno, data); } /* close files */ fclose(fin); fclose(fout); return 0; }
Actually, I'd recommend you NOT call it "listprog" since that's what the other user is calling it.
Experience: BSE Computer Engineering, over 20 years programming experience (various platforms)
The program works as requested. Thanks for providing the additional info, also.
You are most welcome; if you have any questions about this code in the future, feel free to ask.