LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2009, 09:13 AM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Rep: Reputation: 70
Correct syntax for fopen in C?


Hi guys,

Is this scource code snippet correct?

fp=fopen("/home/Desktop/documents/test.txt","r");

I'd have thought these forward slashes in the pathname in this context need to be escaped, do they not?

I mean like this:

fp=fopen("//home//Desktop//documents//test.txt","r");

Which is correct?


Thanks. cc.
 
Old 10-23-2009, 09:30 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Completely Clueless View Post
Hi guys,

Is this scource code snippet correct?

fp=fopen("/home/Desktop/documents/test.txt","r");

I'd have thought these forward slashes in the pathname in this context need to be escaped, do they not?

I mean like this:

fp=fopen("//home//Desktop//documents//test.txt","r");

Which is correct?


Thanks. cc.
The fist one will work - the '/' character does not need to be escaped. As it happens, the second one will also work, since multiple '/'-s get treated as one (or maybe as an implicit '/./', I'm not sure, but I don't think it makes any difference). From a shell, try e.g. `ls /path/to/file' and `ls //path////to////////////////file'. This is useful because you don't have to worry about e.g. string1 + '/' + string2 concatenating to make '/path///to/file'.

John G
 
Old 10-23-2009, 10:16 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
While JohnGraham does touch on the matter, I think it worth pointing out explicitly that the question seems to involve two levels of translation of '/'s. On one level, the C compiler must translate the literal string from its source code form to the runtime character array. It does so according to one or more rules. The second level of translation is at the filesystem/OS level, where the description of a filespec is derived from a string. In this case, a completely different set of rules may apply, and would be independent of the programming language.
To answer the question "Is this scource code snippet correct?", there is no definitive answer except, perhaps, "It depends...".

--- rod.
 
Old 10-23-2009, 10:36 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
If you want to have consistent behaviour across both Windows and Linux, use "rb" mode. If you use just "r", Windows will translate CRLF into LF for you.. which is generally not good for cross-platform reliability.
 
Old 10-23-2009, 11:20 AM   #5
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 899

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by theNbomr View Post
While JohnGraham does touch on the matter, I think it worth pointing out explicitly that the question seems to involve two levels of translation of '/'s. On one level, the C compiler must translate the literal string from its source code form to the runtime character array. It does so according to one or more rules. The second level of translation is at the filesystem/OS level, where the description of a filespec is derived from a string. In this case, a completely different set of rules may apply, and would be independent of the programming language.
--- rod.
Okay, thanks all. Fully understood.
 
Old 10-23-2009, 11:21 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
Hi guys,

Is this scource code snippet correct?

fp=fopen("/home/Desktop/documents/test.txt","r");

I'd have thought these forward slashes in the pathname in this context need to be escaped, do they not?

I mean like this:

fp=fopen("//home//Desktop//documents//test.txt","r");

Which is correct?


Thanks. cc.
Under Windows you would have to write something like


Code:
fp=fopen("C:\\User\\Desktop\\documents\\test.txt","r");
- backslashes, not forward slashes, need escaping.

Actually, you'd have to escape backslashes under Linux too, just under Linux backslashes are just regular characters in a file path, they are not path elements separators.

OTOH, Windows accept forward slash as path separator.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
procmail correct syntax robbtek Linux - Software 1 04-21-2009 10:09 AM
Correct syntax for cu jowa45 Slackware 10 11-01-2007 09:26 AM
Correct Syntax for ethtool kaplan71 Linux - Networking 1 08-21-2007 12:30 AM
What is the correct syntax for Lame darkscot Linux - Software 2 08-14-2006 01:29 AM
what is the correct syntax shanenin Linux - Software 2 10-19-2003 12:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration