-
Help
Hi im new and i was wondering if anyone could help me with this code
Code:
/* *******************************
* author:kevin peters
* revison #:0.1
**********************************/
#include <stdio.h>
#include <stdlib.h>
#define QUIT 5
char filename[80];
int get_menu( void );
FILE *fp;
main()
{
int choice = 0;
/* while() makes sure choice does not equal QUIT */
while (choice != QUIT)
{
choice = get_menu();
if (choice == 1)
{
// creates file
puts("Enter a name for the file:");
gets(filename);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error creating file %s", filename);
exit(1);
}
fclose(fp);
}
if (choice == 2)
{
//edits previous file
puts("Enter a file to edit:");
gets(filename);
if ((fp = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
fclose(fp);
}
if (choice == 3)
{
//reads a file
puts("Enter a file to read:");
gets(filename);
if ((fp = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "No exsiting file.");
exit(1);
}
fclose(fp);
}
if (choice == 4)
{
// deletes previous file
puts("Enter a filename to remove: ");
gets(filename);
if ( remove(filename) == 0)
printf("Succesful deletion of %s", filename);
else
fprintf(stderr, "Error deleting file %s", filename);
exit(1);
}
}
return (0);
}
int get_menu( void )
{
int selection = 0;
/* display menu() to screen */
do
{
printf("\n");
printf("1 - Create entry\n");
printf("2 - Edit previous entry\n");
printf("3 - Read previous entry\n");
printf("4 - Delete previous entry\n");
printf("5 - Quit\n");
printf("\n");
printf("\nEnter what you would like to do: ");
scanf("%d", &selection);
}
while (selection < 1 || selection > 5);
return selection;
}
i keep getting warnings and if this helps too i edit it in visual c++ and compile with borlands c/c++ compiler because visual c++ compiler doesnt work for me
-
Quote:
i keep getting warnings
what are they? what line number?
thatll be alot easier than reading each line to find it.
edit: is it because you dont have 'int main'?
-
main should be defined as either int main() or as int main (int argc, char **argv).
Also, as nadroj said, before we can help you we need the warnings you are getting.
-
That may be one issue though as far as I know the implied type is int. But clearly state int main... I don't feel like trying to compile it myself or reading through so please post up your warnings.
-
i always thought for c that it is only supposed to be main() not int main()
ohh and the warnings are not the choice parts but the if statements in each if statement
-
-
can you copy and paste the warning with the line numbers.
-
heres the warnings:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Warning W8069 35: Nonportable pointer conversion in function main
Warning W8060 35: Possibly incorrect assignment in function main
Warning W8069 49: Nonportable pointer conversion in function main
Warning W8060 49: Possibly incorrect assignment in function main
Warning W8069 62: Nonportable pointer conversion in function main
Warning W8060 62: Possibly incorrect assignment in function main
Warning W8075 67: Suspicious pointer conversion in function main
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
-
main() is valid and standard, because if you don't give a function a return value it is assumed to be int. It is however confusing and bad practice to do this.
-
where you do things like this:
Code:
if (fp = fopen(filename, "r") == NULL)
you should be doing:
Code:
if ((fp = fopen(filename, "r")) == NULL)
and also compare all of your fclose calls.. one of them is wrong.
-
King Mir, that is only in C89 that main can be defined as such. In C99 main should be defined as int main() or int main(int argc,char **argv).
Perhaps you should read the FAQ I posted also.
-
Ok. thanks for that but now im getting the problem of not allowing me to open any files or even edit them. it keeps showing the stderr from fprintf. can anyone help me on this? btw there is no more error in the compiling
-
// suggestion deleted...
edit:
it looks like this was the problem (and, again, wherever similar code exists):
Code:
puts("Enter a name for the file:");
gets(filename);
when i used scanf instead of gets it worked.
notice that you never get a chance to actually type in a filename (or at least it didnt for me) after you print to ask for one.. im sure its something to do with the newline character or some buffer from the initial scanf from the menu.
ive never written a C program before so i dont know the details about which function is better or why this is happening (exactly).
it looks like you should use fgets instead of gets as the latter function is prone to buffer overflows. to keep your program as it is (ie gets or fgets) ive read that you can just use getchar() after your first scanf to clear the buffer.. give it a shot.
-
Using gets() is a bad idea: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
I suspect that when you "used scanf instead of gets" you have also have a buffer overflow, such as this:
Code:
char *str;
scanf("%s", str);