opening a sequence of files

C

csnataraj

Howdy all
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char and run a for loop on the
outside?

Thanks

CSN
 
R

Richard Heathfield

(e-mail address removed) said:
Howdy all
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char

No. It's almost always wrong to cast in a C program, and the places where
it's right are not the places you'd expect.

#include <stdio.h>

int main(void)
{
int i = 0;
while(i < 100)
{
char buf[FILENAME_MAX + 1] = {0};
FILE *fp = NULL;
sprintf(buf, "filename.%d", i);
fp = fopen(buf, "w");
if(fp != NULL)
{
fprintf(fp, "Hello, world number %d\n", i);
if(ferror(fp))
{
/* report the write error */
}
if(fclose(fp) != 0)
{
/* report the close error */
}
}
else
{
/* report the open error */
}
++i;
}
return 0;
}

/* not tested, not even compiled. If it works, great. If not, fix it. */
 
A

Andrew Poelstra

Richard said:
(e-mail address removed) said:
Howdy all
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char

No. It's almost always wrong to cast in a C program, and the places where
it's right are not the places you'd expect.

[progam involving sprintf]

/* not tested, not even compiled. If it works, great. If not, fix it. */

As Richard said, no, casting an integer into a char won't work.

Here's some hackier code that's more efficient, although I don't
recommend using it unless you understand it.

int main()
{
FILE *handle;
char *files = "file0"
int ctr = 0;

for(ctr = 0; ctr < 10; ctr++)
{
handle = fopen(files);
/* File handling code here */
fclose(files);

/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
file[4]++;
}
}
 
A

Andrew Poelstra

Richard said:
(e-mail address removed) said:
Howdy all
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char

No. It's almost always wrong to cast in a C program, and the places where
it's right are not the places you'd expect.

[progam involving sprintf]

/* not tested, not even compiled. If it works, great. If not, fix it. */

As Richard said, no, casting an integer into a char won't work.

Here's some hackier code that's more efficient, although I don't
recommend using it unless you understand it.

int main()
{
FILE *handle;
char *files = "file0"
int ctr = 0;

for(ctr = 0; ctr < 10; ctr++)
{
handle = fopen(files);
/* File handling code here */
fclose(files);

/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
files[4]++;
}
}
 
B

boa

Andrew said:
Richard said:
(e-mail address removed) said:
Howdy all
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char
No. It's almost always wrong to cast in a C program, and the places where
it's right are not the places you'd expect.

[progam involving sprintf]

/* not tested, not even compiled. If it works, great. If not, fix it. */

As Richard said, no, casting an integer into a char won't work.

Here's some hackier code that's more efficient, although I don't
recommend using it unless you understand it.

int main()
{
FILE *handle;
char *files = "file0"
Surely you mean char files[] ?
int ctr = 0;

for(ctr = 0; ctr < 10; ctr++)
{
handle = fopen(files);
/* File handling code here */
fclose(files);

fclose(handle);
/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
files[4]++;
}
}
 
M

Michael Wojcik

Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Would it work to cast the integer into char and run a for loop on the
outside?

Richard has already answered your larger question, but I thought I'd
note that char is an integer type, and you can use it in the
controlling expression of a for loop. While I don't see any
particular reason to do it this way, for the particular case of a
single decimal digit you could do something like:

char Num;
for (Num = '0'; Num <= '9'; Num++)
{
char Filename[20];
sprintf(Filename, "filename.%c", Num);
...

This works for decimal digits because they're guaranteed to be
positive, contiguous, and in order. As I noted above, though,
there's no advantage I can see to doing it this way; I just wanted to
point out that the controlling expression of a for loop doesn't have
to involve an int variable.

--
Michael Wojcik (e-mail address removed)

Against all odds, over a noisy telephone line, tapped by the tax authorities
and the secret police, Alice will happily attempt, with someone she doesn't
trust, whom she can't hear clearly, and who is probably someone else, to
fiddle her tax return and to organise a coup d'etat, while at the same time
minimising the cost of the phone call. -- John Gordon
 
R

Richard Heathfield

Andrew Poelstra said:
Here's some hackier code that's more efficient, although I don't
recommend using it unless you understand it.

And if you understand it, the chances are that you won't want to use it.
Even after you fix all the syntax errors (of which there are a goodly
number), you're still left with some seriously undefined behaviour.
 
A

Andrew Poelstra

Richard Heathfield schreef:
Andrew Poelstra said:


And if you understand it, the chances are that you won't want to use it.
Even after you fix all the syntax errors (of which there are a goodly
number), you're still left with some seriously undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Really? I had to rush home instead of checking it, but even so, I
though I'd done better.

Here's a revised version:

#include <stdio.h>

int main()
{
FILE *fh;
char fname[] = "file0";
int ctr = 0;

for (ctr = 0; ctr < 9; ctr++)
{
fh = fopen(fname, "r");
/* File handling code goes here */
fclose(fh);

/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
fname[4]++;
}

return 0;
}

(I took the time to compile this one)
 
R

Richard Heathfield

Andrew Poelstra said:
Here's a revised version:

#include <stdio.h>

int main()
{
FILE *fh;
char fname[] = "file0";

The array of char is a great improvement, since you are now pointing to
space you actually own, as a result of which...
/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
fname[4]++;

....this line no longer invokes undefined behaviour. It'll stop working as
expected when you get past '9', of course, as you have already indicated.
(I took the time to compile this one)

Good plan.
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
Could anyone suggest a short way to open a sequence of files one at a
time using a for loop?. Say files of the form
filename.0,filename.1,filename.2,...,filename.n.

Build the file names with sprintf() (of snprintf() if you have C99)
Would it work to cast the integer into char and run a for loop on the
outside?

No.
 
E

Emmanuel Delahaye

Andrew Poelstra a écrit :
Here's some hackier code that's more efficient, although I don't
recommend using it unless you understand it.

Good advice. Once you have understood it, the only way is "dont use it
at all !"
int main()
{
FILE *handle;
char *files = "file0"

pointer to a non modifiable string ...
int ctr = 0;

for(ctr = 0; ctr < 10; ctr++)
{
handle = fopen(files);
/* File handling code here */
fclose(files);

/* This line increments the last char of the string "file0", but
it'll bomb if you go past single digits */
file[4]++;

.... hence undefined behaviour. Your code is buggy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top