1,105,636 Community Members

using a char* for file

Member Avatar
terence193
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
DeanMSands3
Posting Whiz
358 posts since Jan 2012
Reputation Points: 57 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 2 [?]
 
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);

Member Avatar
terence193
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

Maybe this solved thread can help you out
passing file pointer around

Member Avatar
terence193
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
terence193
Junior Poster in Training
58 posts since Apr 2008
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: