Thread: Opening User Specified Files in C

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    26

    Opening User Specified Files in C

    Hello,

    I'm having a bit of trouble opening a user specified file in C. When I execute my code, it will not open the file, even though the files are in the same directory. I have even added them under the Resources folder.

    I'm working in Visual Studio 2010. I remember someone told me I had to change some settings for it work, but I cannot remember what it was. The settings are default.

    Anyway here's my code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    
    
    
    int main()
    {
    
    	char fname[10]; //spreadsheet 
    
    	printf("Please enter the name of the SPREADSHEET you want to use: ");
    	fflush(stdin);
    	gets(fname);
    
    	FILE *fp;
    	if ((fp=fopen(fname, "r")) == NULL)
    	{
    		printf("\nCannot open file");
    		getch();
    		exit(1);
    	}
    	
    	fclose(fp);
    
    
    	return 0;
    }
    Any help would greatly be appreciated. If you've had this problem before, and even the slightest adjustment made it work, please share it! Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, you should stop using gets() and fflush(stdin) for reading things in from the keyboard. The FAQ explains why.

    1.5, a buffer size of 10 is hardly enough to specify a filename nowadays. It isn't even enough for archaic 8.3 filenames.

    Second, when you run things from inside the IDE, the "current directory" could be in several places.
    It could be
    - the project root directory
    - the exe directory
    - wherever you've set in the project settings.

    Try specifying an absolute path, when you've made your filename buffer a lot larger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    fflush(stdin);
    gets(fname);
    For starters you should not be using either of these!
    Cprogramming.com FAQ
    Cprogramming.com FAQ

    Next, is the file in the same directory as the one you're running the executable from?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    fflush(stdin);
    Bad! Read Cprogramming.com FAQ > Why fflush(stdin) is wrong and Cprogramming.com FAQ > Flush the input buffer.

    Code:
    gets(fname);
    Bad! Read Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows.

    Try printing a more useful error message, that actually says why you couldn't open the file. Look up the perror function, or use errno and strerror. Just remember to #include <errno.h>.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    26
    Okay okay, I got it the first time haha.

    Yes, the files are all in the same directory. I've tested a similar code that my friend made although his was "hard coded" and his works just fine.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On modern machines you should probably be using something like char fname[255]; ... no kidding I've seen file paths 3 and 4 lines long and they're not at all uncommon on poorly organized systems. The odds are that, as the others have pointed out, your filename is being clipped to 9 characters...

    Also, if you are on a Windows machine, you might be running into a well known mashup from Microsoft... Go into your Control Panel -> Folder options -> View. Locate and uncheck the box titled "Hide extensions for known file types". Bascically if you have "MyDiary.txt", with that box checked you only see "MyDiary" which is a totally stupid thing for them to do. I would not even hazard a guess how often I've found files with names like "MyDiary.doc.txt"... they're actually word processor documents and people can't figure out why they open in the wrong programs...
    Last edited by CommonTater; 05-23-2011 at 12:21 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The files are in the same directory, but that doesn't mean that you are in that directory (and, in fact, the evidence suggests you are not). Either move the files to the directory you are in or move yourself to the directory the files are in, or just specify the full path. (If you don't know where you are, then maybe _getcwd() is for you.)

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    26
    Nope, still nothing.

    I'm not sure how to use the _getcwd() command.

    I am on a Windows machine, and the fact that I do not know is because I've only taken a Intro to C, class. Where this is one part of the assignment. However I can't get it to work at all. We're only limited to what we've learned. Everything mentioned here is new to me.

    So if you could all elaborate a bit more, that would be GREATLY appreciated.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you run a program, you start in ... some directory. Generally that directory is not where the executable lives, but rather what directory you are in when you run the program. For instance, if I am in C:\ and type "notepad" then my working directory will be C:\, because that's where I was when I started the program, as opposed to ... whatever directory notepad.exe lives in.

    Since you are relying on the IDE to start your program, then the IDE is setting the current working directory somewhere. You should either change it to the directory you want it to be, or at the very least figure out what it is so that you can move your file there, or (and this is something we should have started with) make sure you are actually getting the filename you expect, perhaps by printing it out to verify.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pztar View Post
    Nope, still nothing.

    I'm not sure how to use the _getcwd() command.

    I am on a Windows machine, and the fact that I do not know is because I've only taken a Intro to C, class. Where this is one part of the assignment. However I can't get it to work at all. We're only limited to what we've learned. Everything mentioned here is new to me.

    So if you could all elaborate a bit more, that would be GREATLY appreciated.
    Change the setting in Windows... that has nothing to do with your class or C ... it's just Microsoft being stupid.

    What you may discover is that you have a hidden second extension on the filename... it happens a lot.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    26
    I really don't know what I'm doing wrong. I set my files in my same folder where the vcxproj file is. It just won't open no matter what I do.

    CommonTater: I did what you said, it didn't work.

    http://imageshack.us/photo/my-images/62/filest.png/

    That's what my directory looks like. Everything is in there.
    Last edited by Pztar; 05-23-2011 at 01:02 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Pztar View Post
    I really don't know what I'm doing wrong. I set my files in my same folder where the vcxproj file is. It just won't open no matter what I do.

    CommonTater: I did what you said, it didn't work.

    ImageShack® - Online Photo and Video Hosting

    That's what my directory looks like. Everything is in there.

    Everything is in there ... except your program itself, which is in the Debug folder. Did you try moving your text file there? (Not that that's a guarantee of anything, but at least it's somewhere new to try.)

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    26
    Yes, I just tried that. I realized when I debug it says
    ignment3.exe': Loaded 'C:\Users\P\Desktop\Assignment3\Debug\Assignment3. exe', Symbols loaded.

    So i thought I'd put the files in the Debug folder, however that didn't change anything. It can't be anything complicated. I just don't know what it is.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So this is a bit of a memory leak, but you only have to run it once and then remember what it says:
    Code:
    printf("%s\n", _getcwd(NULL, 512));
    That will tell you what directory it is looking for your file in. You should also do something like:
    Code:
    printf("You want to me to load file >%s<\n", fname);
    before the load to make sure your filename is loaded correctly (note that if the < ends up on the next line, you forgot to get rid of the enter-key).

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How exactly are you reading in the filename now?
    If you're using fgets(), did you remember to remove the \n at the end, before using it as a filename?

    The point of using getcwd() is that you should print the answer to confirm that you're looking in the right place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-12-2011, 02:22 AM
  2. Opening a textfile with user supplied filename
    By BeerGut in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2009, 12:25 AM
  3. opening a user selected file
    By SymDePro in forum C Programming
    Replies: 10
    Last Post: 07-14-2009, 01:35 PM
  4. Opening a user-defined file
    By mr_diss in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:47 PM
  5. saving files/opening files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2001, 10:16 PM