Argument type conflict in my code

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’:
Code:
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:

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?
 
The argument argv is an array. Fopen expects a string, not an array.
 
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?
 
Code:
//const char* justreadfile = {"r"};
FILE filetoprint = fopen(argv, 'r');

You want
Code:
FILE *filetoprint =fopen(argv[1], "r");
 
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.


Code:
#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);
}
 
Back
Top