PDA

View Full Version : C help: how do I use a string to specify a filename?


KrazyHorse
15-05-2006, 00:56:23
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)

Cruddy
15-05-2006, 01:39:41
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?

KrazyHorse@home
15-05-2006, 02:33:37
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

KrazyHorse@home
15-05-2006, 02:43:23
Got the answer from Poly

Apparently I don't need quote marks around my filename if it's already a string

KrazyHorse@home
15-05-2006, 02:47:19
Thanks though, Cruddy

:beer:

Cruddy
15-05-2006, 11:10:30
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.

Darkstar
15-05-2006, 21:10:14
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.

KrazyHorse@home
16-05-2006, 04:36:06
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.

KrazyHorse@home
16-05-2006, 04:36:43
It started working once I fixed that.

Sir Penguin
16-05-2006, 16:19:58
It wouldn't look for a variable called /home/mmcevoy/..., because that's not a valid variable name.

SP

KrazyHorse@home
17-05-2006, 03:58:25
Well, it would probably give me that warning then.

Sir Penguin
17-05-2006, 15:47:44
Yeah, it's a syntax error, so it would complain at you and stop compiling.

SP