(e-mail address removed) wrote:
#
# SM Ryan wrote:
# > (e-mail address removed) wrote:
# > # Is there any way to read multiple files (more than 1000 files) and then
# > # write into one single output file using C? Right now in my program, I
# > # have a loop which asks for the filename and writes into the output file
# > # but this is tedious. Imagine typing 1000 filenames...is there a
# > # efficient way to do this??
# >
# > The file name is fopen is an (char*) expression. It can be a
# > string constant or anything else that is (char*). For example
# > to open the file with names like fwxyzDDD,
# > int i; for (i=0; i<1000; i++) {
# > static char F[] = "fwxyz%03d";
# > char f[sizeof F+3];
# > sprintf(f,F,i);
# > FILE *fn = fopen(f,"r"); if (!fn) {perror(fn); continue;}
# > ...
# > fclose(fn);
# > }
# > Or if you have list of file names file (for example the output of
# > the unix find command), you can fgets the file names, and open
# > the file name you fgets.
# >
# > --
# > SM Ryan
http://www.rawbw.com/~wyrmwif/
# > Death is the worry of the living. The dead, like myself,
# > only worry about decay and necrophiliacs.
#
#
#
# Lets consider I have a list of all the file names in a file called
# list.txt. I wrote the following code
#
# fp2=fopen("list.txt","r");
# while ( fgets(line, sizeof(line), fp2) != NULL)
# {
# printf("%s", line);
# strcpy(name,line);
# printf("%s", name);
# printf("%d\n",strlen(name));
Print it out it in the most irritatingly verbose manner possible, like
{char *q=name; for (; *q; q++) printf("<%02X>%c",*q,' '<=*q && *q<127?*q:'.');}
printf("\n");
and then make sure it's not sneaking in any extra non-printing characters,
like the \n fgets leaves at the end of the buffer.
Voluminous output no longer kills trees. If you can't understand what's
happenning print everything so you know what's on whether than guessing.
# fp = fopen(name, "r" );
# fp1 = fopen("out.txt","a");
# if (fp==NULL)
# {
# printf("error in opening\n");
# exit(1);
# }
Most implementations set errno on fopen failure, so you can do
perror(name)
and get both the name you think you're using and the exact error.