1

I am currently trying to get my program to work the way I want. I am currently at the point where I can open up any text file from the command line (an unlimited amount) and display them. I'd like to expand on this and have users enter phrases that format the displayed text. I have previously posted a similar question and I've gotten some great answers, but I cannot use getopt(). How else would it be possible to scan the command line for a "-w" and a number, so "-w5" and a "-s" with no number. Those are the only two things I'd like to be able to detect. I don't mind if statements, I was hoping for the shortest program in my friends, but at this point, I'd just like to get it done. Any ideas? Multiple if-statements was my friend's idea, I personally think this is unneeded, but if that's what I have to do... If anyone else has any ideas, that would be really useful. I just want my program to detect those two characters from the command line. I'm fairly new to C (I've only made a few programs), but I'm edger to learn and I have tried googling and trying this on my own, but being new to C, trying to find what I need through all the other text and jargon is difficult.

Anything will be useful, thanks.

#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)
{





}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Why can you not use `getopt()`? –  Dec 08 '12 at 00:11
  • Because he's on Windows? The user ID is different, but the options being discussed are the same as in [Am I understanding `getopt()` correctly?](http://stackoverflow.com/questions/13767888/am-i-understanding-getopt-correctly). – Jonathan Leffler Dec 08 '12 at 00:18
  • Do a Google (or other search engine of your choice) search on 'getopt source at&t public domain'. You'll find the source code for a version of `getopt()` that was placed in the public domain by AT&T in about 1985. You'll need to convert it to Standard C (it did not use prototypes), but otherwise, you should be good to go. – Jonathan Leffler Dec 08 '12 at 00:20
  • Please learn how to indent your code properly for presentation on SO. Generally, avoid tabs altogether and set tab stops to 4. – Jonathan Leffler Dec 08 '12 at 00:23
  • I wish it was that easy, but I have very little skill in VS and in C in general. My code needs to match my skill level, and if I with getopt(), I'm getting a bit ahead with myself. I just need a tried and true method to getting this done. If not, I'll have to face the music and tell my teacher and my friends I went a little bit overboard with my idea, and take a less then poor grade :( – user1886339 Dec 08 '12 at 00:24
  • The tried, true and easy technique is using `getopt()`. There are all sorts of ad hoc parsing schemes you can find for parsing options, but they're weird and almost invariably buggy in various ways. I'm certainly not going to spend time teaching you how to do it the wrong way; neither are many other people. You can look up other questions related to `getopt()` with a tag `[c]` on SO. – Jonathan Leffler Dec 08 '12 at 00:26
  • 1
    See also: [Is there a replacement for `` for Windows Visual C`](http://stackoverflow.com/questions/341817/is-there-a-replacement-for-unistd-h-for-windows-visual-c), and [`getopt.h`: comiling Unix C code in Windows](http://stackoverflow.com/questions/10404448/getopt-h-compiling-unix-c-code-in-windows) – Jonathan Leffler Dec 08 '12 at 00:38

2 Answers2

1

You should look at plan9's ARGBEGIN and ARGEND macros in their libc.h file (at the very end of the file), to see how it's done (for an example of its usage, see arg(3)).

Alernatively, you can check the suckless implementation of this mechanism, which is very nice (I have re-implemented a version of it which parses arguments even after incorrect flags have been found, but it's not published anywhere. I can publish it if you need that).

7heo.tk
  • 1,074
  • 12
  • 23
0

The command line arguments are in argv, and since argv is an array, the only way to find a specific element inside of it is to iterate through, checking each element until you get the one you want. If you don't want to write all that yourself, it looks like C has a method called 'lfind' in search.h that does this. Here is an example of how to use it. Hope that helps :3.

Also, the GNU documentation for it

Jared Roder
  • 182
  • 2
  • 11