UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » What am i doing wrong...

#1 2009-03-09 06:56 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: What am i doing wrong...

This is my first post every anywhere. so please bear with me.

This is part of my main() function:

...

char *FileOut;
char FileName[30];

printf("Enter diameter of ice drop in mm: ");
scanf("%lf", &DropDiameter);
printf("\nEnter file output name without extension: ");
scanf("%s", FileName);

FileOut = CheckFile(FileName); // check to see if file exists

printf("in Main: %s\n", FileOut); [color="Red"]<- garbage is printed here[/color]


...and my CheckFile():

char *CheckFile(char *file){

char *File;
char NewFileName[30];
char Answer;

//	printf("%s\n",file);
   File = strcat(file,".csv");  // add file association
//	printf("%s\n",File);
   do{
      if ( access(File, 0) != -1 ){  // does file exist?
         fprintf(stderr,"\nFile exists. Override?[y/n]");
         scanf(" %c", &Answer);
   // make sure only proper responses are accepted
      while((Answer != 'y') && (Answer != 'Y') && (Answer != 'n') && 
            (Answer != 'N')){
         fprintf(stderr,"\nWrong response. Override?[y/n]");
         scanf(" %c", &Answer);
      }
      if((Answer == 'n') || (Answer == 'N')){
         printf("\nEnter file output name without extension: ");
         scanf("%s",NewFileName);
         File = strcat(NewFileName,".csv");
//   	printf("in CheckFile fn inside if loop: %s\n",File);
      }
      }else{
//          	printf("in CheckFile fn outside if loop: %s\n",File);
         return(File); // file doesn't exsits
      }
   }while (Answer == 'n' || Answer == 'N');
   return(File); // file will be overridden
}


...the commented out printf() print the proper values in CheckFile(), but apparently i am not passing the right value back to main(). What am i doing wrong?

Thanks for all your help.


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#2 2009-03-09 09:00 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: What am i doing wrong...

When garbage is printed, does the file exist and do you answer "N"?  Because, that
"N" case is definitely wrong: you are building the file in a stack buffer that's local to
your CheckFile() function, and that buffer will disappear as soon as you return from
the function...  However, in the "Y" case or the case that the file doesn't exist, it looks
to me like it should work ok...  It's using the buffer passed in from main(), so that
should be fine...

However, 30 is a bit small for a filename buffer...  Are you sure you're not overflowing
it?  You really should use bounds-checking string functions (fgets() or a length on the
scanf(%s), strncat() or better yet strlcat() if you've got it)...  If you don't, you'll probably
end up regretting it later, if this is real-world code...

Offline

#3 2009-03-09 09:12 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: What am i doing wrong...

Ignoring the potential buffer overlows when giving a file name longer than 25
characters, one problem is:

File = strcat(NewFileName,".csv");

NewFileName is allocated on the stack, strcat just returns the first arg and
File will point to NewFileName which will be gone when the function returns.
So don't do that, just modify the buff pointed to by file and get rid of File and
NewFileName (and make one getfilename function instead of doing it twice).

Your indentation might be wrong too, as it is it doesn't quite align correctly.
At least it's giving me a headache.

As far as I can see it should work when the file doesn't exist, that is at this
point:

//          	printf("in CheckFile fn outside if loop: %s\n",File);
         return(File); // file doesn't exsits

Offline

#4 2009-03-09 09:31 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: What am i doing wrong...


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#5 2009-03-09 10:43 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: What am i doing wrong...

Offline

#6 2009-03-09 11:37 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: What am i doing wrong...

Maybe I should of clarified my question better.

I want the file sent back to main() because i want to sent it to another function to perform writing to an output file.

I'm sure there is a better solution and i'm going about it the wrong way. I guess that's why I'm here.

Your help is greatly appreciated.


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#7 2009-03-10 01:21 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: What am i doing wrong...

Offline

#8 2009-03-10 06:29 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: What am i doing wrong...

Finally have the program working the way it should. Thanks for clarifying everything up for me. I completely forgot about items that are local to functions get destroyed after the function is done. I appreciate all your help and all your time to this matter.


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

  • Index
  • » C
  • » What am i doing wrong...

Board footer

Powered by FluxBB