1.11M Members

using a char* for file

 
0
 

well i am trying to pass this parameter in a function --> char*[I]filename[/I]

[U][B]//i just wrote this as an example to explain clearer my question[/B][/U]
#include<stdio.h>
#include<stdlib.h>

void try (char *[I]filename[/I]) //[B] the question is how can i use the pointer to refer to a file? [/B]
{
	printf("try");
}

int main ()
{
	FILE *f=fopen("lol.txt","w+");
	try(*[I]filename[/I]);	
	return 0;
}
 
0
 

try is a keyword. Also, have you already allocated the memory space for character array pointed to by *filename? (By allocate, I mean like char buffer[20];myFilename=(char*)buffer;try(myFilename); or myFilename=new char[20];try(myFilename);

 
0
 

qm anyways try is just for the example

I havent tried this

char buffer[20];
myFilename=(char*)buffer;
try(myFilename)

but by myFilename i wont be calling the FILE .. and if i change myFilename to *f then i get the incompatible type error.

 
0
 

@zeroliken I have seen that thread before and it is very similar to my case but in my case, I need to pass (char * .. ) and that is what I cant seem to do correctly.


*edit
and char *.. is to resemble a file which is (or should be I think) opened globally.

 
0
 

this is what i attempted so far, but this code has many errors which I cant resolve:

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

typedef struct 
{
	FILE *f
	f=fopen("gamestat.txt","w+");
	char buffer[20];
	*f=(char*)buffer;
} files;

void try2(files);

void try2 (p.char *f) 
{
	printf("try");
}

int main ()
{
	files p;
	try2(p.char*f);	
	return 0;
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: