We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,285 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

using a char* for file

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;
}
3
Contributors
5
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
6
Views
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 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);

DeanMSands3
Posting Whiz
310 posts since Jan 2012
Reputation Points: 80
Solved Threads: 42
Skill Endorsements: 1

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.

terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

@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.

terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 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;
}
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1056 seconds using 2.72MB