tags:

views:

312

answers:

3

Hi i have a simple C question .. :

int main(int argc,char *argv[])
{
  char *text;
  int textLen,repNum;

  text = stream2string(stdin,&textLen);
  //....text = argv[0] doesnt work :(

so how can i read from argv[0]? I use netbeans .. and everytime i have to type in stdin.. when i use argv then the programm will execute without my input.

UPDATE :

Everytime , i start the programm(netbeans green button) .. i have to Type an Example String ! I am lazy.. the string is always the same : ABAABAABBBA So ..i will take the first argument instead of stdin . But argv[1] doesn't work either .. here is :

stream2string :

char *stream2string (FILE *fptr, int *n)
{
  static char *s;

  *n = 0;
  ALLOC(s,char,2);
  s[*n] = getc(fptr);

  while(s[*n]!=EOF && s[*n]!='\0' && s[*n]!='\n') {
   REALLOC(s,char,++*n+2);
  s[*n] = getc(fptr);
   }
  s[*n] = '\0';
  return(s);
} /* stream2string() */

I think .. setting textLen is also important. Can anybody help ?

+2  A: 

Try to use argv[1] instead to read the first argument. argv[0] returns the name of the executable that was called (or the name of the link to the executable on Unix/Linux systems).

rstevens
A: 

This sounds like you haven't set the program arguments for when it is run in the IDE. This is a NetBeans issue - nothing to do with C.

anon
+7  A: 

argv[0] is the name of your executable; I don't think you want to read from that! Rather, I think you want to open argv[1] (the filename given as the first argument to your program on the commandline) and read that:

int main(int argc,char *argv[])
{
  char *text;
  int textLen,repNum;
  FILE *theinput;

  if (argc < 2) {
    /* no argument */
    /* give error message and exit */
    fprintf(stderr, "Must pass an argument!\n");
    exit(1);
  }

  theinput = fopen(argv[1], "r");

  if (!theinput) {
    /* Argument is non-existing file */
    /* give error message and exit */
    fprintf(stderr, "Can't read %s\n", argv[1]);
    exit(1);
  }

  text = stream2string(theinput, &textLen);

  fclose(theinput);

etc. (Of course you can and should provide more detailed and helpful error messages, etc, etc, but I'm trying to focus on the key points that you appeared to be missing).

Edit: ah well, focus seems out of fashion, judging from the comments, so I just edited to provide minimally acceptable error messages.

Alex Martelli
Don't forget to check the return value of fopen before you attempt to read from theinput!
Robert Gamble
OK, added some error checking, was trying to keep it simple (as I see that nobody else's giving any code, I guess that once again this proves how trying to actually be helpful on SO is a mistake;-).
Alex Martelli
use argc first to see if there is at lest 1 argument
hiena
It isn't a mistake and I commend you (and upvoted you) because you provided a clear answer to an unclear question, taking the time to attempt to reasonably interpret the problem. I made my comment because the OP is an apparent newbie and setting good examples for error checking is important, IMHO, especially since I see so many experienced programmers fail to do so.
Robert Gamble
@Robert, good points, though abundant inline error-checks and messages can end up being confusing to beginners (they hide the mainline logic) -- that's the big plus of exceptions, btw, getting error handling "out of the way" to make the mainline stand out more starkly, simply and clearly. Anyway, I gave up on focus and clarity given that @hiena also seems to think that checking in minute detail to provide just the right message is more important than having the OP understand what's going on!-)
Alex Martelli