I have a whole bunch of files (file0000,file0001....file5000), and want to read them all at once in C.
Anybody knows?

Thanks!!

> Anybody knows?
Yes.

Do you know about argc and argv ?

Do you know about wildcard expansion, say
myprog file*

> Anybody knows?
Yes.

Do you know about argc and argv ?

Do you know about wildcard expansion, say
myprog file*

Not really, I am a beginner of C. could you tell me more about that?

Tks for the information.
I actually wrote to read a binary file and write the content to another binary file:

fp =fopen("/home/raw/file0050.dat","rb");
result = fopen("/home/output.dat","wb");
fread(buffer,sizeof(short int),MAXSIZE,data);
fwrite(buffer,sizeof(short int),MAXSIZE,out);
fclose(data);
fclose(out);
It works perfectly.

However, there're about 5000 files in the folder(raw). I want to read all the file at once, extract the content and put them into another binary files (e.g. file0000.dat -> output0000.dat).

Could anybody tell me how to do that?

Please post using code tags (click the [B]#[/B] button and paste your code in between the tags)

>I want to read all the file at once

Do you mean all the files at once, then read this:
Why would you do that? You can't process them all at once :P
But you could do it as follows: you open one file at once, you read the current file's contents, write it to the other file, close the file and open another one, ... (repeat this process until all files have been processed) :)

Or did you mean: I want to read the whole file in one time?

Sorry about the midleading question. As I mentioned before, there are 5000 files in the folder. I want the script read a file (e.g. file0000.dat) and write the output file which is output0000.dat. Then, close the file and open another one, write,....At the end, I will have 5000 output files.
I don't want to type the file name manually in order to make the script open, read and write another file.
I am looking for a script to do that for me.....

>I am looking for a script to do that for me.....
Take a look at this page.
Though you'll have to adapt it a bit, but it implements all the basic stuff already :)

Consider this loop:

for (i = 0; i < 5000; i++)
{
    sprintf(ifile, "file%04d.dat", i);
    sprintf(ofile, "output%04d.dat", i);
        //process the files
}

Look up the pieces of this code to understand what is happening.

Consider this loop:

for (i = 0; i < 5000; i++)
{
    sprintf(ifile, "file%04d.dat", i);
    sprintf(ofile, "output%04d.dat", i);
        //process the files
}

Look up the pieces of this code to understand what is happening.

Thanks for your useful information and I do understand what the code means.
However, there is segmentation fault. The following is my code:

int i;
short int buffer[MAXSIZE];
FILE *in;
FILE *out;
char ifile[20],ofile[20];
/**/
main(){

for (i = 0; i < 5000; i++)
{
	sprintf(ifile,"file%04d.dat",i);
/*Since i goes from 0-5000, file0000.dat to file5000.dat will be written into ifile*/

	sprintf(ofile,"o_file%04d.dat",i);
/*Similar to ifile, o_acq0000.dat to acq0010.dat will be written to ofile*/
	
	in = fopen("C:/Document/ifile","rb");
	out = fopen("C:/Document/ofile","wb");
	fread(buffer,sizeof(short int),MAXSIZE,in);
	fwrite(buffer,sizeof(short int),MAXSIZE,out);
	
	fclose(in);
	fclose(out);
	return(0);

}
}

So, I would like to know where does it go wrong?

for one thing, you're not reading and writing to your numerically sequenced files 'ifile' and 'ofile' like it appears that you think you are doing.

change it to the following

sprintf(ifile,"C:\\Document\\file%04d.dat",i);
sprintf(ofile,"C:\\Document\\o_file%04d.dat",i);

in = fopen(ifile,"rb");
out = fopen(ofile,"wb");

and you'll have to make the file name character arrays larger.


.

for one thing, you're not reading and writing to your numerically sequenced files 'ifile' and 'ofile' like it appears that you think you are doing.

change it to the following

sprintf(ifile,"C:\\Document\\file%04d.dat",i);
sprintf(ofile,"C:\\Document\\o_file%04d.dat",i);

in = fopen(ifile,"rb");
out = fopen(ofile,"wb");

and you'll have to make the file name character arrays larger.


.

It works now!! Thanks!

glad to hear it. the important thing is that you understand what makes the corrected version work correctly, and why.

This question has already been answered. Start a new discussion instead.