Code:
#include <stdio.h>
#include <stdlib.h>
#define optionlist "*************\n\n1. Preorder\n2. Inorder\n3. Postorder\n4. Update\n5. Load\n6. Save\n7. Help\n8. Quit\n\n**************\n"
void load();
FILE *infile;
int main(int argc, char *argv[])
{
int option, readin, test=0;
if (argc == 2)
{
printf("The file you have opened reads: \n");
infile =fopen(argv[1], "r");
test = 1;
while((readin = fgetc(infile)) != EOF)
{
printf("%c", readin);
}
}
printf("\n\n");
printf(optionlist);
scanf("%d", &option);
while (option < 1 || option > 8)
{
system("cls");
printf(optionlist);
scanf("%d", &option);
}
switch (option)
{
case 5:
load();
break;
case 8:
if (test=0 )
{
fclose(infile);
}
break;
}
return 0;
}
void load()
{
int readin;
char filename[13];
system("cls");
printf("Enter the name of the file to be loaded: ");
gets(filename);
infile = fopen(filename, "r");
while (infile == NULL)
{
system("cls");
printf("Check for existance of the file.\nTry entering an existing file.\nEnter the name of the file to be loaded: \n");
}
printf("The file you have opened reads: \n");
while((readin = fgetc(infile)) != EOF)
{
printf("%c", readin);
}
}
i think the problem may be caused in the switch statement but no idea how? the function called load is trying to load a text file read in by the user.. however my program is seemingly skipping over the gets(filename); bit, it gives me an error.. although it says 0 errors in the compile and build.
thanks for your time.
EDIT: after performing some testing.. it doesn't have anything to do with the switch statement.. i'm still working on it.. i'll let u know if i find anything out.. but it aint looking so hot..
EDIT2: after more testin.. it seems as though its got something to do with being in a function.
Code:
#include <stdio.h>
int main()
{
FILE *infile;
char filename[13];
int readin;
printf("\nEnter a file name: ");
gets(filename);
infile = fopen(filename, "r");
while((readin = fgetc(infile)) != EOF)
{
printf("%c", readin);
}
return 0;
}
this works, but when i cut the stuff from this into a function it doesn't work.. i don't really understand..
PLEASE help
cheers guys.
FINAL EDIT: i worked it out. i needed to use scanf instead of gets. i don't really understand why that fixed it? but it did.
maybe an explanation could help me?
i understood the diff between gets and scanf was that gets excepted spaces and ended at a newline, where scanf read a space or newline as the end?