0

I wanted to know how it would be possible to get other input through the command line? I want to look for "-w" and a number, so it would look like "-w60" and "-s". This input is given through the command line, so it would look like this:


c:\Users\Username\Desktop> wrapfile.exe -w5 -s test.txt

Output should look like this:

Hello , this is a test

What the -w5 and -s mean is:

-w5 = width (can only display 5 characters at a time)

-s = spacing (Include spacing, so fit as many whole words as you can)

I want to create a function that scans for these two characters, and if anyone knows how to format the output so it does what it needs to do, that would also be helpful.

I'm just a wee bit confused, I've been working on this program for a while and I just want to learn how these things can be scanned and used properly.

Here is my current code that takes in an unlimited amount of text files from the command line:

#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int l = 1; while(l != argc) { FILE *fp; fp = fopen(argv[l], "rb"); l++; if (fp != NULL) { int i = 1; do { i = fgetc(fp); printf("%c",i); printf(" "); } while(i!=-1); fclose(fp); } else { printf("Error.\n"); } } } /* void scanningForWS(int argc, char **argv) { } */ 
7
  • 4
    Ugh! Please don't use a lowercase L as an identifier, thanks. Commented Dec 7, 2012 at 16:31
  • 1
    stackoverflow.com/q/13755035/905902 Ugh! Commented Dec 7, 2012 at 16:38
  • Lol I'll keep that in mind :) If you choose to copy and paste the code, just let me know you changed it if you display an answer :D Commented Dec 7, 2012 at 16:43
  • Yeah, I wanted to improve on that idea. I realized you needed to pass a variable to define how much width you wanted. Commented Dec 7, 2012 at 16:45
  • @pmg, why so? (please read the "Variable names" section on the following document: doc.cat-v.org/bell_labs/pikestyle ) Commented Dec 7, 2012 at 16:58

2 Answers 2

2

If you pass -w5 -s test.txt to your program your argv's are:

argv[0] = "wrapfile.exe" argv[1] = "-w5" argv[2] = "-s" argv[3] = "test.txt" 

So:

int l = 1; fp = fopen(argv[l], "rb"); 

is not what you want for sure.

For illustration purposes... in order to print to a "restricted" width you can do something like this:

char * h = "this is a string longer than width"; // you'd get this from your file int width = argv[1][2] - '0'; // you wouldn't hardcode this... int count; for(count = 0; count < strlen(h); count++){ if((count % width) < width - 1) printf("%c", str[count]; else printf("%c\n", str[count]; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Somewhat, I would like it to work together, so if the user passes -w5 -s test.txt, I'd like the c program to detect the fact that the user passed -w5, and -s, and then the c program with the help of a function limits the width to 5, and includes spacing when displaying the output of test.txt. I just want to know how that function can be created, or even if it can be created lol
@JamesHeartly: take a look at getopt(), or roll your own.
Actually, I think getopt() is exactly what I was looking for, thank you.
@JamesHeartly - I agree getopt() works nicely, but you still have to do the handling for each case yourself. Once you've identified the -w option you can strip the number as I'm doing in the example code (note the edit) then you can loop and print to whatever width you'd like
0

I find getopt cumbersome to use. Writing your own tests is not too difficult. For example:

#include <string.h> #include <stdio.h> int main(int argc, char **argv) { int haveSpacing = 0; int haveWidth = 0; FILE *fp = 0; while (*++argv) { if (!strcmp(*argv, "-s")) { // check for -s switch haveSpacing = 1; } else if (sscanf(*argv, "-w%d", &haveWidth) == 1) { // check for -wxx } else if (**argv == '-') { // reject anything else beginning with "-" printf("invalid switch %s\n", *argv); return 1; } else if (argv[1]) { // filenaname must be last arg, so arg[1] must be NULL printf("invalid arg %s\n", *argv); return 1; } else if (!(fp = fopen(*argv, "rb"))) { // open last arg, the filename perror(*argv); return 1; } } if (!fp) { printf("missing filename\n"); return 1; } // ... return 0; } 

1 Comment

Hey Joseph Quinsey, I was wondering what this test actually does? Would it just scan the command line for -wxx and -s? Just curious, I tried it out and I couldn't figure it out lol :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.