Sponsored Content
Homework and Emergencies Homework & Coursework Questions trouble understanding file option and command line arguments Post 302558629 by heywoodfloyd on Saturday 24th of September 2011 03:01:08 PM
Old 09-24-2011
trouble understanding file option and command line arguments

Hi,

I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read.

this is the code I have so far:

Code:
/* wc simulate */

#include <stdio.h>
#include <stdlib.h>

char *pgmname; /* name of this program */

int line_count = 0;
int word_count = 0;
int byte_count = 0;

FILE *fp;

void main(argc, argv)

int argc; char *argv[];
{


int i; 
char *cp;

pgmname = argv[0];
fp = stdin;

for(i = 1; i < argc; i++) {
        cp = argv[i];
        if(*cp == '-'){
                if(*++cp == '\n'){
                        line_count++;}
                
                
        }

        else {
                  if(fp != stdin) {
                   fprintf(stderr, "%s: too many arguments\n", pgmname);
                   exit(1);
                }

                fp = fopen(cp, "r")
                if(fp == NULL) {             
                   fprintf(stderr, "%s: unable to read %s\n", pgmname, cp);
                   exit(1);
                }

        }

                printf("%d\n", line_count);
}

                

}

Right now I have code to just count the lines. I am testing the program on a hello world program below:

Code:
#include <stdio.h>

main()
{
printf("Hello World!\n");
}

when I test the program, I get 0 for line count.

California State University, Northridge, USA, Prof Gabrovsky, Comp 322
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Little Trouble Understanding some code...

Couple of questions as I try to decipher someones code who left... What would something coded like this do? IFS=: grep FIELD1 /Path/Path2/Param.fle | read LBL1 LBL2 USRID EADR SUBJ SERVERNAME CFGTBL DIR ERR=0 Param.fle contents.. FIELD1:FEI::FIELD2:dATAFIELD BATCH:MAIN SERVER......etc.. (2 Replies)
Discussion started by: NycUnxer
2 Replies

2. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

3. UNIX for Dummies Questions & Answers

Having trouble understanding this command: >foo<bar bc

Sometimes it works for me and sometimes I get this error: syntax error on line 1, teletype Basically I've got no idea whats going on, especially at the end of the command: bc Any help is appreciated (1 Reply)
Discussion started by: phunkypants
1 Replies

4. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

5. UNIX for Dummies Questions & Answers

Trouble managing ports from the command line

What are the commands to manage ports from my command line. Example: Opening Ports, Closing Ports, Viewing their status, etc. I am having a hard time finding this answer. I'm trying to trouble shoot some networking problems and it would be very helpful if I could just do this from the... (1 Reply)
Discussion started by: syregnar86
1 Replies

6. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

7. Homework & Coursework Questions

Make a file accept only two arguments from the command line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) The script is executed in the Korn shell. 2) Name the shell script file is asg6s. 3) The asg6s file is... (7 Replies)
Discussion started by: ProgMan2015
7 Replies

8. Fedora

Understanding Killall command , wait option

:wall:killall -wHi i need to understand how the -w option works in terms of processes. If this command is issued does it literally terminate all running processes ..wait for them all to be terminated and then return to standard output ? Thanks in advance (2 Replies)
Discussion started by: MrRobot
2 Replies

9. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. What is on Your Mind?

I'm having trouble understanding what it is I need to do

Ok, thank you. Again I'm new to the programming thing, I'm just trying to figure out what exactly it is I need to do. How would I cash out bits? Numerous questions. I'm a dreamer. AI attempts to communicate with me regularly especially through unfinished apps that I'm assuming is my responsibility... (1 Reply)
Discussion started by: C-lo
1 Replies
sgml-spell-checker(1)						       SGML						     sgml-spell-checker(1)

NAME
sgml-spell-checker - SGML spell checker SYNOPSIS
nsgmls -l yourdoc.sgml | sgml-spell-checker [option] ... DESCRIPTION
sgml-spell-checker is a tool that you can use to automatically spell-check your SGML documents. One of the advantages of this tool over some other SGML-aware spell checkers is that it scans your documents in the form in which the SGML parser actually sees it, which means it is not line-based, system entities are resolved, marked sections are treated appropriately, etc. Also, this tool can be made aware of particular DTDs, in the sense that it knows not to spell-check the content of elements that do not represent human-language text, such as <programlisting> in DocBook. An exclusion list for the DocBook DTD is included, others can be added trivially. The input to sgml-spell-checker is the text representation of your SGML document's Element Structure Information Set as generated by nsgmls (from SP or OpenSP; sometimes installed under the name onsgmls). In other words, you need to pipe the output of nsgmls into sgml-spell- checker as shown in the synopsis. Provide to nsgmls the options you need, such as -c to search more catalogs, -i to include a marked sec- tion, or more source files. Do not forget the -l option, or you won't get any file or line references for the misspellings. The second part of the pipe takes a couple of options; see below. Note that if the language of the document does not match your system's locale settings, you need to use the --language option. The output of sgml-spell-checker is a list of the words that are misspelled (in the opinion of aspell), together with file name and line number. Note that the line number designates where the element that contains the word started, not where the word actually is. So most likely you will have to search a few lines below the indicated location. OPTIONS
--debug Debug mode. Generates lots of output not of interest to the normal user. --language=language Sets the language of the document. (The format depends on the aspell installation, but something like en or en_US should work.) By default the language is taken from the system locale settings. --suggestions Shows correction suggestions for misspelled words. --dictionary=file Uses an additional aspell dictionary file. This option may be used multiple times. --dtd=dtd Uses the exclusion list for the specified DTD (e.g., docbook). --help Shows a brief help, then exits. EXAMPLES
nsgmls -l -D . mydoc.sgml | sgml-spell-checker --language=en --dtd=docbook --dictionary=mydict1.aspell --dictionary=mydict2.aspell (You can enter this command all on one line without the backslashes, or on several lines with the backslashes.) NOTES
Read the aspell documentation about how to set up the appropriate dictionaries. In case you're having trouble interpreting the aspell doc- umentation, here's how to make an aspell dictionary file from a flat word list: rm -f mydict1.aspell # aspell won't overwrite existing files aspell --language-tag=xx create master ./mydict1.aspell < mywordlist.txt Watch the slashes. aspell likes to see a slash in the name or it will search some default location. BUGS
This program should be able to identify the language from the document (e.g., <book lang="de">), but aspell doesn't handle changing the language on the fly. AUTHOR
Peter Eisentraut (peter_e@gmx.net) SGML spell checker 2004-09-19 sgml-spell-checker(1)
All times are GMT -4. The time now is 12:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy