Counterglow Forums  

Go Back   Counterglow Forums > General Discussions > Technoglow

Reply
 
Thread Tools Display Modes
Old 15-05-2006, 00:56:23   #1
KrazyHorse
Habs Fan
 
KrazyHorse's Avatar
 
Join Date: Dec 2001
C help: how do I use a string to specify a filename?

I'm going to be outputting data to a bunch of different files with slightly different names.

The output filename should be of the form

/home/mmcevoy/3year/princcomp/ver2/tempnumber.dat

where number is an integer I'm pulling out of a list (and temp stays as a literal part of filename)

so, first I define the C-string

char dat[] = ".dat\"";

then I define my list of numbers as list

I set i to a specific integer and test by assigning

char tfilename[200];
sprintf(tfilename, "\"/home/mmcevoy/3year/princcomp/ver2/temp%d%s", list[i], dat);

when I do

printf("%s", tfilename);

the output reads

"/home/mmcevoy/3year/princcomp/ver2/temp1238.dat" (1238 is the right number)

but when I do

FILE *filet;
filet = fopen(tfilename, "w");
fclose(filet);

it compiles, but I get a segfault when I attempt to run

(using gcc compiler)
KrazyHorse is offline   Reply With Quote
Old 15-05-2006, 01:39:41   #2
Cruddy
Good days and bad
 
Cruddy's Avatar
 
Join Date: Apr 2005
Location: Vice City
Quote:
sprintf(tfilename, "\"/home/mmcevoy/3year/princcomp/ver2/temp%d%s", list[i], dat);
I'm shit at C, but isn't there just 3 sets of quotes in that line?
__________________
We will bite them on the features
Cruddy is offline   Reply With Quote
Old 15-05-2006, 02:33:37   #3
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
don't know what you mean?

there are 3 double quote marks in the line

the first opens the formatting string in the sprintf command

the second follows the escape character \ so that it actually prints a double quote mark at the beginning of the string tfilename

the third closes the formatting string in the sprintf command

in addition, my definition of dat causes a double quote mark to be placed at the end of the string tfilename
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 15-05-2006, 02:43:23   #4
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
Got the answer from Poly

Apparently I don't need quote marks around my filename if it's already a string
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 15-05-2006, 02:47:19   #5
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
Thanks though, Cruddy

__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 15-05-2006, 11:10:30   #6
Cruddy
Good days and bad
 
Cruddy's Avatar
 
Join Date: Apr 2005
Location: Vice City
NP - it just leapt out at me that you had an odd number of quotes. It's like on/off switch, you have to have even number of quotes?

That's way I was taught anyway. Probably obsolete with today's compilers.
__________________
We will bite them on the features
Cruddy is offline   Reply With Quote
Old 15-05-2006, 21:10:14   #7
Darkstar
will bitch for beer
 
Darkstar's Avatar
 
Join Date: Nov 2001
Location: Rocket City
Do you need quotes in the filename? I don't recall ever using them with filenames, on any platform. But I'm spoiled to using objects that know what the hell I mean these days.

Also, do you need the Unix / or a MS-DOS \ ? If you need Windows, you should change your slashes to back-slashes, and then double them to make C happy.
__________________
> clue++;
> display clue;
-878923403

Last edited by Darkstar; 15-05-2006 at 21:12:19.
Darkstar is offline   Reply With Quote
Old 16-05-2006, 04:36:06   #8
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
I'm in linux, so / all the way

If you call fopen it requires a string as the first argument. If you haven't previously defined a string as your designated filename then you require quotes around the filename so that it doesn't try to parse the string as a variable (it would look for a variable called /home/mmcevoy/... and, not finding one, would complain.

If you've actually defined a string and are calling it by its variable name then the compiler already knows that it's a string and so doesn't look for a variable with that name, so quotes aren't necessary.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 16-05-2006, 04:36:43   #9
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
It started working once I fixed that.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 16-05-2006, 16:19:58   #10
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
It wouldn't look for a variable called /home/mmcevoy/..., because that's not a valid variable name.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Old 17-05-2006, 03:58:25   #11
KrazyHorse@home
Top of the East
 
KrazyHorse@home's Avatar
 
Join Date: Nov 2003
Location: 12-3-1
Well, it would probably give me that warning then.
__________________
Whenever you see a dp, you should see a 2Pi. If you don't, then you screwed up.-Prof. Feldman
KrazyHorse@home is offline   Reply With Quote
Old 17-05-2006, 15:47:44   #12
Sir Penguin
Wise sprite
 
Sir Penguin's Avatar
 
Join Date: Nov 2001
Location: command.com
Yeah, it's a syntax error, so it would complain at you and stop compiling.

SP
__________________
Whether 'tis nobler in the plane to suffer
The asps and adders of outrageous fortune,
Or to take arms against a sea of bitch-ass motherfuckers
And by opposing fuck them up?

-- Samuel L. Jackson as Hamlet
Sir Penguin is offline   Reply With Quote
Reply
Forum Jump

Thread Tools
Display Modes

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 On
HTML code is Off



All times are GMT. The time now is 00:41:47.


Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© Counterglow 2001-2012. All rights reserved.