Manzer
Manzer

Reputation: 1

Correct usage from function prototype in program

I have to use the following C function in one of my programs (using Kiel compiler):

Prototype: int fopen (FILE* f, char* filename, char* mode)
Parameters: 
1. f-Pointer to file structure
2. filename-Pointer to a memory location that contains the filename.
3. mode-Pointer to a memory location that contains the file open mode.
Return Value: 1, if file was opened successfully.
0, otherwise.

When I tried this I am getting error:

FILE * f;
char* filename;
char* mode;
int t;


filename[0]= 'g';

mode[0]='w';


t= fopen( f, filename[0],mode[0]);

Error:

COPYRIGHT Copyright (C) 2012 - 2013 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** ERROR C141 IN LINE 171 OF F34x_MSD_F931DC_main.c: syntax error near 'FILE'
*** ERROR C202 IN LINE 171 OF F34x_MSD_F931DC_main.c: 'f': undefined identifier
*** ERROR C141 IN LINE 172 OF F34x_MSD_F931DC_main.c: syntax error near 'char'
*** ERROR C202 IN LINE 172 OF F34x_MSD_F931DC_main.c: 'filename': undefined identifier
*** ERROR C141 IN LINE 173 OF F34x_MSD_F931DC_main.c: syntax error near 'char'
*** ERROR C202 IN LINE 173 OF F34x_MSD_F931DC_main.c: 'mode': undefined identifier
*** ERROR C141 IN LINE 174 OF F34x_MSD_F931DC_main.c: syntax error near 'int'
*** ERROR C202 IN LINE 174 OF F34x_MSD_F931DC_main.c: 't': undefined identifier
*** ERROR C202 IN LINE 177 OF F34x_MSD_F931DC_main.c: 'filename': undefined identifier
*** ERROR C202 IN LINE 179 OF F34x_MSD_F931DC_main.c: 'mode': undefined identifier
*** ERROR C202 IN LINE 182 OF F34x_MSD_F931DC_main.c: 't': undefined identifier

C51 COMPILATION COMPLETE.  0 WARNING(S),  11 ERROR(S)

Can someone help me in correct usage ?

Update:

When I put the variable declarations at the beginning in the main I managed to remove all the errors. But a new error is coming now:

COPYRIGHT Copyright (C) 2012 - 2013 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** ERROR C214 IN LINE 184 OF F34x_MSD_F931DC_main.c: illegal pointer conversion

I got some hint here, but even then unable to understand how to resolve this issue

Upvotes: 0

Views: 444

Answers (4)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16650

There are several mistakes, that let me think, that you are not experienced with C-pointers. Therefore, beside your question:

a) All pointers are uninitialized. This will lead to undefined behavior.

b) All pointers points to unreserved memory. This will lead to undefined behavior.

c) filename and mode are char * and considered to be strings. They must be 0-terminated.

With a higher relationship to the subject of your question:

d) You do not pass a pointer, but the first element of an array, the pointer should point to (and does not).

Probably you want to do something like this:

t= fopen( f, "g", "w");

This works, because string literals are char * in C.

e) There is already an fopen() in C.

f) If you have qour own fopen() it should porbably return an entity of type FILE * via parameter list. There are two ways to do this:

You have an entity of the type FILE and pass a pointer to this:

 FILE file;
 t= fopen( &file, "g", "w");

fopen() creates that entity, so you have an entity of type FILE * and pass an pointer to this:

FILE *file;
t= fopen( &file, "g", "w");

I highly recommend to spent further time on learning, how pointers work in C. It is not easy.

Upvotes: 0

Manas
Manas

Reputation: 598

int fopen (FILE* f, char* filename, char* mode)

This means you should pass a pointer-to-char to argument filename, but you are passing filename[0], which is a char. The same happens with argument mode.

The code below will do:

FILE* f;
char* filename="file.txt";   //assuming the file you want to write in is called file.txt and in the same folder with the project
int t;

t= fopen(f, filename, "w");

However, I think you should take some time to fully understand some basics about C. You didn't malloc any memory for the pointers, which tends to cause some runtime error. And you seem a little messed up with types. It's better to have a solid foundation before dealing with files, or there will be more problem.

Upvotes: 2

Santhosh Pai
Santhosh Pai

Reputation: 2625

The correct way of passing the arguments is

            t= fopen(f, filename, mode);

Upvotes: 1

Eric Finn
Eric Finn

Reputation: 8985

Here, filename and mode are both char*s (pointers to char). However, you're giving fopen filename[0] and mode[0], both of which are chars.

char* filename;
char* mode;
t= fopen( f, filename[0],mode[0]);

The correct way would be to pass the char*s:

t= fopen(f, filename, mode);

This isn't causing your compilation error, but it will cause run-time errors: You aren't allocating memory for your pointers before dereferencing them. You can solve this a few ways:

1: Declaring them as arrays:

char filename[2] = "g";
char mode[2] = "w";
...

2: Dynamically allocating memory:

char * filename = calloc(2,1);
char * mode = calloc(2,1);
filename[0]= 'g';
mode[0]='w';
...
free(filename);
free(mode);

Upvotes: 0

Related Questions