Thread: Opening User Specified Files in C

  1. #16
    Registered User
    Join Date
    May 2011
    Posts
    26
    Well I got it to work.. in an odd way.

    I had to specify the file extension too.. ".txt"

    However I do not want to do this, is there a way around this?

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Pztar View Post
    Well I got it to work.. in an odd way.

    I had to specify the file extension too.. ".txt"

    However I do not want to do this, is there a way around this?
    Create a file that doesn't have an extension.

    You do realize we are all going to come to your house and smack you for going all this time without actually once typing in the actual name of the file you want to open, right? Right.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.
    Yes... everything except the executable... move your txt files to the debug directory, where the executable file is or specify ..\filename.ext when entering your filenames...

    And don't undo that windows change. Seriously, you'll find it a big help everywhere...

  4. #19
    Registered User
    Join Date
    May 2011
    Posts
    26
    Yes, I apologize. All the other codes I've tried worked fine without the extension.

    But Thank you for all the help I REALLY appreciate it

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pztar View Post
    Well I got it to work.. in an odd way.

    I had to specify the file extension too.. ".txt"

    However I do not want to do this, is there a way around this?
    Yep...

    Code:
    if (! strchr(filename,'.'))
      strcat(filename,".txt");
    That should work either with or without the extension.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pztar View Post
    Yes, I apologize. All the other codes I've tried worked fine without the extension.

    But Thank you for all the help I REALLY appreciate it
    Probably because you were creating files without extensions... something of a no-no on the Windows platform. It defeats the entire purpose of File Associations where you click a file to open it in it's correct program. (another reason to see extensions)

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Except that Windows (newer versions, anyway) seems to default to kindly removing the extension from the Explorer window, which leads to n00bs in positions like this.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Except that Windows (newer versions, anyway) seems to default to kindly removing the extension from the Explorer window, which leads to n00bs in positions like this.
    Hense the settings change I told him to make in the first part of the thread...

    Control Panel -> Folder Options -> View tab -> uncheck "Hide extensions of known file types" ...

    The stupid thing defaults to on, which is probably half the problem. People get used to not seeing extensions and soon enough they start creating files without them, even being bothered by seeing them. Then the entire File Associations mechanism, which is why why files have extensions in the first place, starts breaking down and next thing you know it's "Format C:\" time, yet again.


    Afterthought: How the heck do you expect to write programs for an OS you don't even know how to use?
    Last edited by CommonTater; 05-23-2011 at 04:28 PM.

  9. #24
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by CommonTater View Post
    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...
    You were given good advice in several ways by CommonTater.

    If using a C99 compatible compiler, consider using: char fname[FILENAME_MAX];

    FILENAME_MAX is defined in the <stdio.h> and is defined for the longest filename that an implentation will open.

    I do not have a copy of the C90 standard to determine if this macro was defined in <stdio.h> in that version of the language standard also.

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