hi every one!

i have a question, how to pass arguments from other functions to main. i want to write a program like nano well not exactly like nano editor. I have a function f_read(char* filename[]), and fopen() get filename[1] as file name and *filename[2] as "r" read mode and rest of the code will read from a file.

I want is this char filename[] to main(int argc , char argv[])

how can i do that??

any help will be appreciated

Recommended Answers

All 14 Replies

The main() function is the first function run in a C program (not necessarily so in a C++ program). You can't just pass output from another function to main(), but you can call them from main() and use the output of them for whatever needs you have.

so that means what ever i have to do is to be in main function right ?

No. That means your programs will start in main() (at least appear to start to us), in C. In fact, C programs may not start in C, (Pelles C doesn't), but in thinking about where the program starts in C, "it starts in main() in C".

Usually, it's best to pass variables from main() to the functions that need them. If the variables will be changed, then pass them by using the & (address of) operator:
myFunction(&someIntVariable)

or by creating a pointer to the variable, and then passing the pointer:

int *pointer=&numberVariable;
myFunction(pointer);

or

char myCharArray[30];
myFunction(myCharArray); //the name of the char array servers as a
//constant pointer to the base of the array.

The way to work with functions is to use them to handle one part (and only one part), of the programs. For example, you might typically want functions for:

input (getting the data)
processing (more than one function will be needed, usually)
output (to the screen, to a file, perhaps to a printer)

By breaking the program down into smaller functions, you make it easier to program, and MUCH easier to debug and modify, later on.

so you're saying that

void f_read(char* f[]);
int main(int argc ,  char* argv[])
{

   f_read(&argv);//called function to read from file
}

void f_read(char* f[])
{
    f = fopen(f[0],f[1]); //f[0] is file name and f[1] is mode in which you         wish to open 
    //rest of the logic goes here
}

am i right ???

Nope. ;)

argv is a 2D char array. You can have 2D char arrrays written as *name[], (like argv is), or name[size1][size2]. They aren't EXACTLY the same, but they're so close, you can consider them to be the same thing, for now.

So when you call a function, you don't use myFunction(&argv). That would send the address of an address, of a 2D array - and the compiler will error out on it. Remember that the name of the char array IS the address of the array.

You want myFunction(argv) - if you want to send the entire array to myFunction, and myFunction(argv[n]) if you want to send one string from the *argv[number] array.

The fopen(f[0],f[1]) - I've used a char array for the filename, but never used it for the file mode. If you have the syntax right, it should work fine, however: "r" (remembering the " ").

What I'd recommend is first, get your program to compile - then print out *argv[1], and *argv[2], and see EXACTLY what you have.

If you get stuck, post back.

ok what i did is in here

void r_file(char*[]);
int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("no file name given\n");
    }

    if(argv[2] == "r") // check what's the 3rd argument is if it's r then call f_read() but it gives me warning "comparison with string literal results in unspecified behavior"
    {
        r_file(argv);
    }

 return 0;
}
void r_file(char *fname[])
{
    FILE *fp;
    char ch;
    if((fp = fopen(fname[0] , "r")) == NULL)
    {
        printf("can't oprn file <%s>\n",fname[1]);
        fclose(fp);
        exit(1);
    }
    else
    {
        while((ch = fgetc(fp)) != EOF)
        {
            putch(ch);
        }
        fclose(fp);
    }
}

what i'm still not getting ??

Strings don't work like that in C, they're really just arrays under the hood, so stuff like the == operator won't work like you expect. There are standard functions that support strings in C, and you can find them in the string.h header. Specifically for your needs, the function is strcmp.

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

void r_file(char*[]);

int main(int argc, char *argv[])
{
    if (argc != 3) {
        printf("no file name given\n");
    }

    if (strcmp(argv[2], "r") == 0) {
        r_file(argv);
    }

    return 0;
}

void r_file(char *fname[])
{
    FILE *fp;
    char ch;

    if ((fp = fopen(fname[0], "r")) == NULL) {
        printf("can't oprn file <%s>\n", fname[1]);
        fclose(fp);
        exit(1);
    }
    else {
        while ((ch = fgetc(fp)) != EOF) {
            putchar(ch);
        }

        fclose(fp);
    }
}

line 14:r_file(argv);

Wrong. That is passing the entire array of strings, not just a single string. You have to tell it which string you want to pass. I'm assuming the filename is located at argv[1]

r_file(argv[1]);

void r_file(char*[]);//note: expected 'char **' but argument is of type 'char *'
int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("no file name given\n");
        exit(1);
    }

    if(strcmp(argv[2], "r") == 0)
    {
        r_file(argv[1]);//warning: passing argument 1 of 'r_file' from incompatible pointer type
    }
    else
    {
        printf("\nmode is not given");
        exit(1);
    }

 return 0;
}
void r_file(char *fname[])
{
    FILE *fp;
    char ch;
    if((fp = fopen(fname[0] , fname[1])) == NULL)
    {
        printf("can't oprn file <%s>\n",fname[1]);
        fclose(fp);
        exit(1);
    }
    else
    {
        while((ch = fgetc(fp)) != EOF)
        {
            putch(ch);
        }
        fclose(fp);
    }
}

why didn't you change the parameter of r_file() from char** to char*???

what do you mean? is char** or char*[] is 2D array ??

Yes but that's not what it should have. A filename such as "myfile.txt" is char*, not char** or char*[].

argv is char**, which is an array of strings. If you only pass one string such as argv[1] then you are passing char*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.