file loading problem which i think may be caused by my switch statement

This is a discussion on file loading problem which i think may be caused by my switch statement within the C Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    file loading problem which i think may be caused by my switch statement

    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?
    Last edited by jibbles; 08-31-2004 at 04:18 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    scanf() is a nasty function which shouldn't be used if you can help it! Specifically it has a
    habit of leaving the new line in the input buffer. So when you did gets() (by the way you
    should really use fgets() - see the FAQ for reasons why) you read in filename as just the
    newline that was left in the input buffer from when you did the scanf(). For more info on
    getting input from the user, look at the FAQs.

    Another btw: it is good practice to always fclose() a file that you fopen(). It is also a good
    idea to use a different FILE* variable when you open a different file. At the moment you
    could have the case where you enter a file name at the command line which opens a file,
    then choose to open a file which overwrites the 'infile' variable and then exit without
    fclose()ing either of them. Its probably not a huge deal for this level of program, but in the
    long run its usually best to do things properly!

    always worth doing
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 05:21 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21