How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Tony M Your Own Question

Tony M
Tony M, Computer Software Engineer
Category: Programming
Satisfied Customers: 41
Experience:  BSE Computer Engineering, over 20 years programming experience (various platforms)
17418655
Type Your Programming Question Here...
Tony M is online now
A new question is answered every 9 seconds

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.
Submitted: 5 years ago.
Category: Programming
Expert:  Tony M replied 5 years 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;
}


Expert:  Tony M replied 5 years ago.

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


Tony M, Computer Software Engineer
Category: Programming
Satisfied Customers: 41
Experience: BSE Computer Engineering, over 20 years programming experience (various platforms)
Tony M and other Programming Specialists are ready to help you
Customer: replied 5 years ago.

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

Expert:  Tony M replied 5 years ago.

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


JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)
 
 
 

Related Programming Questions