# Argument type conflict in my code



## lockfile (Oct 5, 2010)

I am trying to write a program that reads a text file into a buffer (that has max size of 1024 bytes). Then the program will print out the entire file on the command line. It is a primitive program that I am writing to learn more about C. I can understand C code but I have never written anything useful so I'm taking baby-steps to get to the point where I ultimately have a useful program.

My problem is that I am having problems with the various types that deal with the simplest operation within C. I tried to open a file with "FILE filetoprint = fopen(argv, 'r');" but I got this error " In function â€˜mainâ€™:

```
openandprint.c:16: warning: passing argument 1 of â€˜fopenâ€™ from incompatible pointer type
/usr/include/stdio.h:249: note: expected â€˜const char * __restrict__â€™ but argument is of type â€˜char **â€™
openandprint.c:16: warning: passing argument 2 of â€˜fopenâ€™ makes pointer from integer without a cast
/usr/include/stdio.h:249: note: expected â€˜const char * __restrict__â€™ but argument is of type â€˜intâ€™
openandprint.c:16: error: invalid initializer
```

here is my code:


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

int main(int argc, char *argv[]) {

// fopen(*argv[]); 
int argsoncline = 0;
printf("\n");
for(argsoncline; argsoncline < argc; argsoncline++)
	if (argsoncline > 0) 
		printf("%s ", argv[argsoncline]);
printf("\n\n");

//const char* justreadfile = {"r"};
FILE filetoprint = fopen(argv, 'r');

//check if file open was successful
//if (Filetoprint < 0 )
//	printf("file open failure");
	
return(0);
}
```

You guys got some suggestions?


----------



## derekschrock (Oct 5, 2010)

You need to understand what char *argv[] is.

http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html


----------



## SirDice (Oct 5, 2010)

The argument *argv* is an array. Fopen expects a string, not an array.


----------



## kpa (Oct 5, 2010)

fopen(3) returns a FILE *, a pointer to a something of type FILE.


----------



## lockfile (Oct 5, 2010)

Okay. Here is my recap. argv in main() is a pointer of type char ** that points to a char * [] so *argv[n] is the (beginning of the string that represents the) nth argument to the program, that is also the nth string in array argv. And, *argv[n] is of type char* []. Is that about right?


----------



## SirDice (Oct 5, 2010)

That sounds about right.


----------



## rlp1938 (Oct 5, 2010)

```
//const char* justreadfile = {"r"};
FILE filetoprint = fopen(argv, 'r');
```

You want 

```
FILE *filetoprint =fopen(argv[1], "r");
```


----------



## lockfile (Oct 5, 2010)

*ok project finished*

Ok it's done. One thing that really irks me about C is that if I pass an argument to a program by value *data_val instead of data_val then C will complain of a type mismatch. Even though the type is correct, C will say the type is incorrect because it was passed by value *data_val rather than data_val and varies for methods that modify the data. It's almost like the require the address rather than *data_val. Which is weird. So I guess *data_val is the same as passing by value.  Anyway it compiles fine with GCC. If you guys wanna try it out. It's my primitive version of cat  only capable of catting <= 1KB.

I wanted to have some error correction in it for example to check if the fopen() and fread() were successful and print data telling the user that the respective function failed IF I can get the fread() and fwrite() functions to cooperate with me. What is the solution when man fread doesn't have enough information or explanation to empower me to code? Man pages are meager on info.



```
#include <stdio.h>

int main(int argc, char *argv[]) {

int argsoncline = 0; 
printf("\n");
for(argsoncline; argsoncline < argc; argsoncline++)
	if (argsoncline > 0) 
		printf("%s ", argv[argsoncline]);
printf("\n\n");

//const char* justreadfile = {"r"};
FILE *filetoprint = fopen(argv[1], "r");

char textfromfile[1024];
fread(&textfromfile, 1024, 1 , filetoprint); 

//print the items that we read from the file
printf("%s\n", textfromfile);

return(0);
}
```


----------



## Alt (Oct 7, 2010)

You forgot fclose().


----------



## lockfile (Oct 8, 2010)

Oh yes thanks. I added the snippet below into the code:


```
fclose(filetoprint);
```



			
				Alt said:
			
		

> You forgot fclose().


----------

