473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C and default file types

I'd like to know how to open a document in a default app. like for example
if I wanted to open a .txt document I'd like my program open notepad if it
is the default for a windows machine text files or open openoffice if it is
the default application for linux.

is there a portable way to do this?
Nov 14 '05 #1
8 1490
i was trying to do it for webpages as well and sending http://www.google.com
to the command processor doesn't do anything but generate an error message.
Nov 14 '05 #2
In article <hf*******************@fe2.texas.rr.com>,
Jerry Gaspard <jg********@hotmail.com> wrote:
I'd like to know how to open a document in a default app. like for example
if I wanted to open a .txt document I'd like my program open notepad if it
is the default for a windows machine text files or open openoffice if it is
the default application for linux.
is there a portable way to do this?


There is no such a thing as a "default app" associated with a
file in a UNIX system. You may build such an association
into your C program. Here's something to get you started with.
It will require quite a bit of enhancements to become something
workable.

enum filetypes { unknown, text, html, jpeg, gif };
char *default_apps[100];

default_apps[text] = "cat";
default_apps[html] = "w3m";
default_apps[jpeg] = "xv";
default_apps[gif] = "xv";
....
default_apps[unknown] = (you decide)
To determine the file type of a file, see the man page for the "file"
command on Linux and the references in its "See Also" section.

---------- WARNING! WARNING! -----------------------------------

Having said that, I urge you strongly to resist the temptation of
giving a "default app" functionality in your program. In general
such functionality opens a slew of possibilities for security breaches.
It's sufficient for one file to masquerade as something other than
what it is and have your program gullibly run an unexpected program.
DON'T DO IT!

--
rr
Nov 14 '05 #3
> There is no such a thing as a "default app" associated with a
file in a UNIX system. You may build such an association
into your C program. Here's something to get you started with.
It will require quite a bit of enhancements to become something
workable.

enum filetypes { unknown, text, html, jpeg, gif };
char *default_apps[100];

default_apps[text] = "cat";
default_apps[html] = "w3m";
default_apps[jpeg] = "xv";
default_apps[gif] = "xv";
...
default_apps[unknown] = (you decide)
To determine the file type of a file, see the man page for the "file"
command on Linux and the references in its "See Also" section.

---------- WARNING! WARNING! -----------------------------------

Having said that, I urge you strongly to resist the temptation of
giving a "default app" functionality in your program. In general
such functionality opens a slew of possibilities for security breaches.
It's sufficient for one file to masquerade as something other than
what it is and have your program gullibly run an unexpected program.
DON'T DO IT!

--
rr


the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?
Nov 14 '05 #4
"Jerry Gaspard" <jg********@hotmail.com> writes:
the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?


This is off topic in comp.lang.c, (hence the email, not posting) but
what you may want is the Windows "start" command. If you do

start http://www.foo.com/bar/xyzzy.html

then that will fire up the user's default browser and open that URL

In C you would do

sprintf(buf, "system %s", url);
system(buf);

with buf appropriately declared and url holding the URL you want to open.

Gnome and KDE on Unix-like systems *might* have similar commands but
I'm not sure.
Nov 14 '05 #5
"Jerry Gaspard" <jg********@hotmail.com> writes:
[...]
the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?


Even assuming the question is meaningful, there is no way to do what
you're trying to do in portable C. On some systems (Windows?), there
may be a well-defined mapping between file types and applications, but
the way to determine that mapping is entirely implementation-specific.
On others, the mapping may be part of the user environment, and the
operating system itself may have no awareness of it. On yet others,
there may be no such mapping at all, but your program could provide
such a mapping (but I don't think that's what you're looking for).

The question is outside the scope of this newsgroup. Try a newsgroup
specific to the system(s) you're working with.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
>> Having said that, I urge you strongly to resist the temptation of
giving a "default app" functionality in your program. In general
such functionality opens a slew of possibilities for security breaches.
It's sufficient for one file to masquerade as something other than
what it is and have your program gullibly run an unexpected program.
DON'T DO IT!

I second this opinion.
the main thin i want is to use the default web browser,
What's a default web browser? A web browser is started up with a
command like "lynx", "mozilla", "netscape", or whatever. There's
no browser named "default" that I've ever heard of (and in some
shells, it's treated as a builtin command for part of a switch/case
construct). If you want to start one, name it in a command.

i don't want to open
an IE window with firefox is the users default browser or i don't want to
"open with <program name>" is an alien concept invented by the virus
writers primarily to make viruses easier to run.
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?


Files don't have associated MIME types system-wide. I suppose you
could say all files have the associated MIME type
virus/malicious-dont-you-dare-run-this. There's a reason for having
execute permission bits on files, and most files *DON'T* have them
set. Even root needs at least one executable permission bit set.
An Apache *SERVER* will make such MIME-type associations. I have
no idea why the knowledge that a file with extension .html is
associated with MIME type text/html will help you with the problem
you are talking about.

I also think the idea of having *ONE* app associated with a MIME type
is silly. There are generally at least TWO apps that use a particular
file type: one produces them and one uses them.

Gordon L. Burditt
Nov 14 '05 #7
In article <zA******************@fe1.texas.rr.com>,
Jerry Gaspard <jg********@hotmail.com> wrote:

the main thin i want is to use the default web browser, i don't want to open
an IE window with firefox is the users default browser or i don't want to
open netscape when their default browser is opera. is there a way to tell
what browser, like what MIME type is associated with the .html file type?


Don't assume that there is such a thing as a "default browser".
I suggest that you set up your program so that it prompts the user,
asking for which browser to use. You may provide a menu of options
if you think that makes your program more user-friendly. In that
case, you should allow the user to enter a choice other than those
you have suggested. In any case, avoid the temptation of making
decisions for the user. Experienced users don't like that. Let the
user have control on, and the responsibility for, his/her actions.

--
rr
Nov 14 '05 #8
Berry Kercheval <be***@kerch.com> writes:
This is off topic in comp.lang.c, (hence the email, not posting)


Ooops. Apologies for fumble-fingering the wrong button.

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
10
by: Edward Diener | last post by:
Is the default 'false' for the CLSCompliant attribute at the assembly level ? Are there any tools to check for CLS compliance for the various NET languages ?
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
9
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument....
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
4
by: Grey0 | last post by:
I have a problem. When I doubleclick to open a file folder, it defaults to SEARCH. When I right click on the folder, SEARCH is set as the default...the option to EXPLORE is listed, but when I open...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
7
by: =?Utf-8?B?Y291Z2FyaXN0aWM=?= | last post by:
I am trying to convert an C# application to VB and have one issue which is converting the generic value to the default. C# uses return default(T) as the return value how would I translate this in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.