Login|Contact Us
Question and Answer

Programming

Ask a Programming Question, Get an Answer ASAP!

  • Ask A Question
  • Browse Answers
  • Meet The Experts
  • How JustAnswer Works

Write a C program that takes a command line argument that is

 

Customer Question

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.

Submitted: 1419 days and 7 hours ago.
Category: Programming
Value: $20
Status: CLOSED
Picture
Expert:  Tony M replied 1419 days and 6 hours ago.


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


Accepted Answer

Picture
Expert:  Tony M replied 1419 days and 5 hours ago.


Actually, I'd recommend you NOT call it "listprog" since that's what the other user is calling it.


Expert TypeComputer Software Engineer
Category: Programming
Pos. Feedback: 100.0 %
Accepts: 35
Answered: 5/17/2009

Experience: BSE Computer Engineering, over 20 years programming experience (various platforms)

Ask this Expert a Question >
Customer replied 1418 days and 1 hours ago.

The program works as requested. Thanks for providing the additional info, also.

Picture
Expert:  Tony M replied 1418 days ago.


You are most welcome; if you have any questions about this code in the future, feel free to ask.


 
Tweet

4 Programmers are Online Right Now

Ask Your Question Now
Programming Questions Date Submitted
is there a python programming person available 3/28/2013
Write a menu-driven program that allows users do two options: Option 3/27/2013
1. Which one of the following control structures provides for 3/27/2013
JavaScript: Multiple Choice Questionnaire 3/25/2013
RA-211 3/24/2013
How do you create a searchable public Google drive folder in 3/24/2013
RA-211 3/24/2013
I am an entrepreneur with no current coding skills, but an 3/23/2013
I have base code in VBA that opens all excel files from a folder 3/23/2013
Program in C++ 3/23/2013
RSS
Next 10 >
Ask A Programmer
Type Your Programming Question Here...
characters left:

Top Programming Experts

See More Programmers

In The News

Nbc
Washington Post
New York Times
Cnn
Learn More

How It Works

  • Ask an Expert
  • Get a Professional Answer
  • Ask Followup Questions
  • 100% Satisfaction Guarantee
Learn More
close
Find Expert answers related to your question.
Sign up using email
We will never post anything without your permission.
Already have an account? Sign in

Ask a Programmer

Get a Professional Answer. 100% Satisfaction Guaranteed.
206 Programmers are Online Now
Type Your Programming Question Here...
characters left:
Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.
Truste
Contact Us | Terms of Service | Privacy & Security | About Us
© 2003-2013 JustAnswer LLC