Quick C question...

Status
Not open for further replies.

TCD

Ars Tribunus Angusticlavius
8,220
ALright... I have an assingment where in a subfunction I prompt the user to input a filename then I return that filename to my main function where I can then call that filename to other functions.. I was going to use pointers, but I'm still learning how to use em so..

I need help on how to correctly format the prototype
int getFname(int*);

in main

int *tfname;
tfname = getfname();


and heres the function
int getFname(int *)
{
char filename[41];

printf("\n Please Type name of input file with the appropiate extension and press ENTER.\n");
gets(filename);

return(&filename);

}

Thanks
TCD
 

sittidae

Ars Centurion
325
Subscriptor
Your getFname prototype says that it takes a pointer to int as a parameter and returns an int. You are calling it with no parameters and returning a pointer to pointer to character (you probably just wanted to return a char pointer). You also have a prottype mismatch between "int getFname... and int *tfname; tfname=getFname". But, on top of that, you are returning an address that is on the heap (the non static variable "filename" in getFname) which is not guarnteed to have the value you stored in it.
 

TCD

Ars Tribunus Angusticlavius
8,220
thanks, heres the entire code...
note Im just learning so if you could tell me a little bit more about where I'm being stupid...

I was able to get this to compile, but it wont open the file.
code:<HR>
Code:
#include <stdio.h>
#include <string.h><P>void errorIn(void);
void errorOut(void);
void Header(void);
void OpenFile(FILE **, FILE **, int);
int getFname();<P>void main (void)
{
	int *tfname;
	FILE *pIN;
    FILE *pOUT;<P>	Header();
	
	
	tfname = getFname();
		<P>
	OpenFile(&pIN,&pOUT,tfname);
    <P>}<P>
void OpenFile(FILE **pIN,FILE **pOUT,int filename)
{
   (*pIN)=fopen("tfname","r");
	if((*pIN)==NULL)
	{
	errorIn();
	}<P>
   (*pOUT)=fopen("income.out","w");
	if((*pOUT)==NULL)
	 {
     errorOut();
	}
}<P>void errorOut(void)
{
	  printf("**********The output file could not be opened.*************\n");
      exit(1);
	
}<P>void errorIn(void)
{
      printf("**********The input file could not be opened.*************\n");
      exit(1);
}<P>void Header(void)
{
printf("welcome\n");
}<P>
int getFname()
{
	char filename[41];
	
	printf("\n Please Type name of input file with the appropiate extension and press ENTER.\n");
	gets(filename);<P>	return(&filename);<P>}

so yeah, I thought I was setting the pointer to the filename as an address, but appearently it doesnt like that... so how would I set it up and return a character string and then be able to call that in my OpenFile function...

oh yeah, I'm also not supposed to have any global variables either...

Thanks for the help guys

update...
Im just a little confused on the pointers, if sittidae or anybody else could explain to me what I am fucking up on...
many thanks

[This message was edited by The Cool Dude on November 11, 2001 at 00:16.]
 

gumbo

Ars Tribunus Angusticlavius
6,107
Could it be that your reference to the file passed into OpenFile is used with quotes, making C think it is a filename, instead of the reference? I am not terribly familiar with the fopen syntax, but I think you don't need them. Also, why don't you just use a pointer to pass the filename around (it looks like its what fopen wan't to accept anyways...). Now would be a great time to learn.

gubmo

Disclaimer: I am a noob C++ programmer, and never learned C, so :p
 

wct

Ars Centurion
235
OK, firstly, your declaration types are wrong. You have declared getFname as:

int getFname()

which means it will return an int. However, in the function you have it return a pointer to the character array where the filename is kept. So this should get you at least a compiler warning.

The more important thing is that you're approaching the problem in a bad way. The space you allocated for the filename is local to the function. If you return a pointer to this space, as you are doing now, there is no guarantee it will persist once the function returns, ie the contents will not be what you expect. You can get around this by changing the buffer declaration to static, but this is messy.

To sum up so far, a quick fix to your program would be to declare getFname as:

char *getFname(void);

and in the function getFname, declare the filename as:

static char fileName[54];

I would not recommend this however. To me, static variables are messier than globals. A better way was suggested by Euzeka. In the main function where you are calling getFname from, declare the buffer space, then give this buffer to your function to fill. This involves changing the calling function and the getFname function similar to this:

void main()
{
char fileName[54];
...
getFname(fileName);
...
}

and getFname:

void getFname(char *fileName)
{
gets(filename);
}

Of course this doesn't handle your buffer overflow problems, but that's a different kettle of fish altogether :D
 

TCD

Ars Tribunus Angusticlavius
8,220
alright.. I got the program to compile with 1 warning but it still wont open the file

any ideas why?

code:<HR>
Code:
void OpenFile(FILE **, FILE **, char *);
void getFname(char *);<P>
getFname(filename);
OpenFile(&pIN,&pOUT,filename);<P>void OpenFile(FILE **pIN,FILE **pOUT,char *filename)
{
   (*pIN)=fopen(filename,"r");<P>void getFname(char *filename)
{
fgets(filename, 54, stdin);
}
:confused:

oh and many thanks guys...
Im madly reading my CS book too
:D
 
That's how I would do it too, but he said he doesn't want to use pointers.
Well, I think you can also do:
code:<HR>
Code:
void getFname(char theName[50]);
That's a little misleading since the parameter isn't really by value... but by "reference". It's obvious there, but can get tricky with typedefs on char arrays.

Edit: removed some bad stuff after looking up fopen ;)
Edit2: As for it not opening... your code seems ok. Try replacing the fopen call with one with a hardcoded file name. That might help you in finding out if it's the name that's not getting there, of if it's fopen that's failing for some reason (wrong file name? wrong path? etc).
 

0

Ars Scholae Palatinae
609
Using fgets to get your filename is the problem.

fgets terminates when either an EOF or a newline is encountered. If a newline is encountered, fgets will encorporate that into the string.

Since you are using ENTER to stop the fgets, your string ends up looking like "filename\n\0" instead of "filename\0". These two strings are obviously not alike. So when the program goes to open the file, it looks for "filename\n", which doesn't exist. The behavior of fopen says that if a file being opened for reading doesn't exist, fopen will return an error.

Long story short, fgets is screwing you.
 

Joseph K

Smack-Fu Master, in training
53
Subscriptor++
Originally posted by mebeSajid:
My $.02:

Quote: " gets(filename); "

The man pages for this say DO NOT USE THIS FUNCTION.

It has something to do with the way it buffers input.
You can parse the char array from fgets so that you eliminate the newline.

Sajid

gets() does no bounds checking, so it's easy as pie to overwrite your buffer. Bad guys can take advantage of this to smash your stack and potentially wreak all kinds of havoc, so please, *AVOID gets()*!
 
OK, check these things:

1. You're unnecessarily complicating things by declaring double pointers in your open file function. Change it (and the declaration of the function) to:

code:<HR>
Code:
<P>void main() {
  ...
  OpenFile( pIN, pOUT, fileName );
  ...
}<P>
void OpenFile(FILE* pIN, FILE* pOUT, char* filename) {
  pIN = fopen(filename,"r");
  if(pIN == NULL) { errorIn(); }
  
  pOUT = fopen("income.out","w");
  if(pOUT == NULL)
  { errorOut(); }
}

2. Now that your code has been vetted, it's time to debug it the old fashioned way with printf statements. Put printf statements before after you get the filename from getFname() to see what the result was:

getFname(fileName);
printf("Filename returned from getFname(): %s\n", fileName);

You may want to put more printf's at other points in the program to verify it is doing what it's supposed to. Does this match what you entered? Does the file exist? Where does the error happen (ie which error message are you getting?) You have to start tracing where your program went wrong. And what was the compiler error you got?

EDIT: added the call to OpenFile()
 
Status
Not open for further replies.