Thread: Too easy?

  1. #1
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Unhappy Too easy?

    I'm sure this will be too easy and I'll feel like a stupid fool after a few posts But I'm getting a wee bit ........ed off with myself.
    I want to open a file then check to see if the file name is correct and open it. The code compiles ok but always tells me that it can't open the file

    Code:
    FILE * fptrHistoryFile;  //Declares a global file pointer
    
     
     void main()
    {
    char HistoryFile[50], rightFile, n, y;
    char * pHistoryFileLabel;
    	
    	
            do{
    	
    
    	puts("Please enter the name of the file that is to be checked e.g History.txt \n");
           	gets(HistoryFile);                 
    	pHistoryFileLabel = HistoryFile;  //Assigns a poiter to the file name to be opened.
    	
    	printf("%s will now be checked. Is this right? \n", pHistoryFileLabel);
    	puts("enter 'y' to continue or 'n' to input target file again \n");
    	rightFile = getchar();    //Checks the file name is right.
    	
            }while( rightFile == n );
            
            
            fptrHistoryFile = fopen("pHistoryFileLabel", "r");
           	if( fptrHistoryFile == 0)
    	{
    		puts("An error occured when opening the file ");
    		exit(1);          //Checks the file is valid if not exits the program
    	}
            
            
                  
            
            
    
    
    }
    If anyone could throw some advice or suggest something to try i would be VERY grateful Please.
    Thanks

    Capulet

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > void main()

    main returns an integer

    > fptrHistoryFile = fopen("pHistoryFileLabel", "r");

    Take the quotes off of "pHistoryFileLabel"

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Like Govtcheez said...

    I'll suggest you one more thing, instead using gets(), you should try to use fgets().

    Search a little bit about fgets().

    Code:
    fopen("pHistoryFileLabel", "r");
    Hmm, "pHistoryFileLable"?
    maybe without the " ".

    Good luck.

  4. #4
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Unhappy

    Cheers for the quick reply and the advice.
    But I still can't get the file to open succesfully might I be typing in the wrong file location? Do you have to pass the full location of the file e.g

    I:\Projects\C Programming\Project_2002\History.txt

    or is it just History.txt
    Sorry to keep annoying you but it's really getting to me.
    Thanks

    Capulet

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    you'll need to specify the full path.

    History.txt wont work, instead you make some changes.
    Anyway, try passing the full path to the file, like:

    Code:
    C:\Programming\Cpp\History\History.txt
    If it still don't work, tell use how did you write the path, and we'll see what's the error

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: you'll need to specify the full path.

    Originally posted by Vber
    History.txt wont work, instead you make some changes.
    Anyway, try passing the full path to the file, like:

    Code:
    C:\Programming\Cpp\History\History.txt
    If it still don't work, tell use how did you write the path, and we'll see what's the error
    No. Remember, you are dealing with strings which are characters. As such, the \ is an escape character. Used by itself, (single \) it escapes the following character. Thus:
    C:\Programming\Cpp\History\History.txt
    That's what you end up with. This is what you want:
    Code:
    C:\\Programming\\Cpp\\History\\History.txt
    Or perhaps...

    Code:
    C:/Programming/Cpp/History/History.txt
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Yeah I forgot it..

    but I did try c:\file.txt
    and it did work well, anyway, you're right

  8. #8
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Unhappy I'm thick? :(

    Cheers for all of your advice
    But I STILL get the message that I can't open the file. Is my code ok?
    I keep the file I want to open at

    I:\Project\history_1.txt

    I've tried all of the ways that have been suggested but it still dishes out the same ****e back to me
    Thanks for all of your help anyway you guys are the best



    Capulet

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    You did try to open the file, like this:

    You did try to open the file, like this, or not?

    Code:
    I:\\Project\\history_1.txt

  10. #10
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Talking Happy little boy :)

    Yeah I tried opening it that way with no joy.
    I put the file in the same folder as the .exe and changed the files name from history_1.txt to history.txt and now it works fine

    I can type in history.txt without typing in the full location and it works just fine.

    Thanks very much for all of you guys help I will try to keep in mind what you have told me in future.
    Cheers


    Capulet

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if( fptrHistoryFile == 0)
    This should be
    >>if( fptrHistoryFile == NULL)

    And use perror() to print a meaningful error message when the open fails.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Unhappy Shafted again!!

    I really am a nob!!!

    I thought my program was sorted but, no!
    I've made the changes that Hammer suggested but it still won't open the file! Might it be my compiler? " Miracle C" I got it here
    It was free but is it that which is causing me problems? I know I should get a good one but I've just started C ( you can probably tell? ) so I've not bought one yet.
    Any help is really appreciated
    Thankyou all


    Capulet

  13. #13
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Thumbs up Time for a change

    Ok I'm going to get a new compiler now! and see if that sorts it out.
    I think I'll try the Bloodshed Dev-C++ it sounds like it does the job well enough. I've had too many problems with "miracle" C
    Thanks again for your help I'd go mad without it


    Capulet

  14. #14
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    If this was in dos or a console window, then it might have been the 8.3 file format. For example, a file named

    history_1.txt can be opened for reading or writing as history~1.txt provided the file already exists.

  15. #15
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Miracle C?! ::laughs:: That was my first compiler - I'd have to go with the "horribly broken" statement of Salem's.

    Wow, but thanks for letting me reminisce...

    >>Cygwin Project (C/C++)

    Last I checked that was just a *nix emulator. Sure, it has most of the programming utilities, but you don't need a 600+MB download to code when a small one will suffice (after all, it's just GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM