473,802 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading multiple files

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??

Thanks

May 24 '06 #1
15 15077
On 24 May 2006 08:33:51 -0700, le******@gmail. com 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??

Thanks


<OFF-TOPIC>
You are asking, "how?"
The first question that comes to my mind is "why?"
This is trivial to do with the built in facilities of most command
shells. What problem are you trying to solve? Why do you want to do
it "using C"?
</OFF-TOPIC>
May 24 '06 #2
I was thinking of using C because I can do better in C compared to
other languages. Could you please brief about the built in facilities
of the command shells to do this task?

Roberto Waltman wrote:
On 24 May 2006 08:33:51 -0700, le******@gmail. com 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??

Thanks


<OFF-TOPIC>
You are asking, "how?"
The first question that comes to my mind is "why?"
This is trivial to do with the built in facilities of most command
shells. What problem are you trying to solve? Why do you want to do
it "using C"?
</OFF-TOPIC>


May 24 '06 #3
le******@gmail. com 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.
May 24 '06 #4
le******@gmail. com wrote:
Could you please brief about the built in facilities
of the command shells to do this task?
Roberto Waltman wrote:
le******@gmail. com wrote:
>Is there any way to read multiple files (more than 1000 files) and then
>write into one single output file using C?


<OFF-TOPIC>

This is trivial to do with the built in facilities of most command
shells. What problem are you trying to solve? Why do you want to do
it "using C"?


<OFF-TOPIC>
Let's assume you want to concatenate all "*.log" files in a directory
into a single file:
In a Windows 2000 command window:
copy *.log all_together.no w
In Linux/Unix/???BSD under a bash shell:
cat *.log >all_together.n ow
It is very easy to extend this to copy only files whose names match a
pattern, or where modified between specific dates, or in several
directories, etc.
But these issues have nothing to do with the C language, please post
again in a newsgroup dedicated to the computer platform you are using.
</OFF-TOPIC>
May 24 '06 #5

SM Ryan wrote:
le******@gmail. com 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,lin e);
printf("%s", name);
printf("%d\n",s trlen(name));
fp = fopen(name, "r" );
fp1 = fopen("out.txt" ,"a");
if (fp==NULL)
{
printf("error in opening\n");
exit(1);
}
.....
.....
.....

The file pointer returns null resulting in "error in opening". when I
checked the string length of the filename its always 2 characters more
then the original length. Could you please tell me how I can use the
variable "name" (or "line") in fopen.

May 24 '06 #6
Before I tackle your question, could I just say that I'm rather concerned
that the reaction to your perfectly legitimate question was to say: (a) you
should use your shell to do this, and (b) shells are off-topic, clear off.
If that isn't hostility, I don't know what is.

Folks, we have no evidence that this guy even /has/ a shell that can
concatenate many files into one large one. The C Standard imposes no such
requirement on implementations . Your assumption that he has a shell is
completely off-topic, and those who made it should be ashamed of
themselves.

It's the worst kind of "topic-cop" behaviour - a bit like sneaking a bag of
class A drugs into a guy's pocket and then busting him for possession.

The question is reasonable and, in my opinion, topical.

Okay - the priority is to get the filenames into some kind of
computer-readable list, and le******@gmail. com appears to have achieved
that. So now he just has a tiny problem getting in his way. The fix,
le******@gmail. com, is easy, you'll be glad to hear. See below.

le******@gmail. com said:

<snip>
Lets consider I have a list of all the file names in a file called
list.txt. I wrote the following code
Excuse me a minute:

char *end = NULL;

Thanks. (All will become clear shortly!)

fp2=fopen("list .txt","r");
The fopen call might fail, in which case fp2 will be NULL. You should check
for this.
while ( fgets(line, sizeof(line), fp2) != NULL)
Make sure line is at least FILENAME_MAX + 1 bytes in size.
{
printf("%s", line);
strcpy(name,lin e);


Yes, you've got the filename. Unfortunately, you've also got a newline
character. One easy way to get rid of it is:

end = strchr(name, '\n'); /* find the newline */
if(end != NULL)
{
*end = '\0'; /* chop it off! */
}
else
{
you might want to wonder about why it didn't find a newline; basically, it
means that the filename was so long it couldn't all fit in the buffer, in
which case the rest of the filename will be handed to you on the next
fgets call. Messy. Better to make sure your buffer's big enough to start
with (see above).
}

I hope that helps.

--
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)
May 24 '06 #7


le******@gmail. com wrote On 05/24/06 12:29,:

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,lin e);
printf("%s", name);
printf("%d\n",s trlen(name));
fp = fopen(name, "r" );
fp1 = fopen("out.txt" ,"a");
if (fp==NULL)
{
printf("error in opening\n");
exit(1);
}
....
....
....

The file pointer returns null resulting in "error in opening". when I
checked the string length of the filename its always 2 characters more
then the original length. Could you please tell me how I can use the
variable "name" (or "line") in fopen.


When fgets() reads a line, it stores the entire line
including the '\n' at the end. Since the name of the
file you are trying to open is probably "foo.txt" and not
"foo.txt\n" , you need to search for and remove that '\n'
character. Something like this will do it:

char *p;
...
p = strchr(line, '\n');
if (p != NULL)
*p = '\0';

This much would explain one extra character per line,
but you've reported seeing two. It may be that you've mis-
counted, and that simply removing the '\n' will solve your
problem. A more troublesome possibility is that "list.txt"
may have originated on a system that uses a different line-
ending convention from yours, and that it wasn't properly
translated to your system's conventions when it was moved
there. For example, Windows systems terminate lines with
the two-character pair '\r','\n', while POSIX systems use
'\n' by itself. If the '\r','\n' pairs weren't translated
to single '\n' characters, then the lines you read from the
file will look like "foo.txt\r\ n"; removing the '\n' will
leave you with "foo.txt\r" , and fopen() will probably still
fail.

If this is the case, the best cure is to clean up the
procedure that brought you "list.txt" in the first place, so
the line endings will be translated properly. If that's not
possible, you can add a little more code to remove '\r' (if
it's present), just as the code above removes '\n'. This
isn't perfectly bullet-proof, though, because some systems
use even stranger line-ending conventions than '\r','\n' --
for example, a system that used '\n','\r' would produce a
file that if read on a POSIX system would look like

first line\n
\rsecond line\n
\rthird line\n
...

It's for reasons like this that I recommend revisiting your
file-transfer process instead of trying to outguess the
remote system's line-ending conventions.

--
Er*********@sun .com

May 24 '06 #8
le******@gmail. com wrote:
#
# SM Ryan wrote:
# > le******@gmail. com 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,lin e);
# printf("%s", name);
# printf("%d\n",s trlen(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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Where do you get those wonderful toys?
May 24 '06 #9

Eric Sosman wrote:
le******@gmail. com wrote On 05/24/06 12:29,:

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,lin e);
printf("%s", name);
printf("%d\n",s trlen(name));
fp = fopen(name, "r" );
fp1 = fopen("out.txt" ,"a");
if (fp==NULL)
{
printf("error in opening\n");
exit(1);
}
....
....
....

The file pointer returns null resulting in "error in opening". when I
checked the string length of the filename its always 2 characters more
then the original length. Could you please tell me how I can use the
variable "name" (or "line") in fopen.


When fgets() reads a line, it stores the entire line
including the '\n' at the end. Since the name of the
file you are trying to open is probably "foo.txt" and not
"foo.txt\n" , you need to search for and remove that '\n'
character. Something like this will do it:

char *p;
...
p = strchr(line, '\n');
if (p != NULL)
*p = '\0';

This much would explain one extra character per line,
but you've reported seeing two. It may be that you've mis-
counted, and that simply removing the '\n' will solve your
problem. A more troublesome possibility is that "list.txt"
may have originated on a system that uses a different line-
ending convention from yours, and that it wasn't properly
translated to your system's conventions when it was moved
there. For example, Windows systems terminate lines with
the two-character pair '\r','\n', while POSIX systems use
'\n' by itself. If the '\r','\n' pairs weren't translated
to single '\n' characters, then the lines you read from the
file will look like "foo.txt\r\ n"; removing the '\n' will
leave you with "foo.txt\r" , and fopen() will probably still
fail.

If this is the case, the best cure is to clean up the
procedure that brought you "list.txt" in the first place, so
the line endings will be translated properly. If that's not
possible, you can add a little more code to remove '\r' (if
it's present), just as the code above removes '\n'. This
isn't perfectly bullet-proof, though, because some systems
use even stranger line-ending conventions than '\r','\n' --
for example, a system that used '\n','\r' would produce a
file that if read on a POSIX system would look like

first line\n
\rsecond line\n
\rthird line\n
...

It's for reasons like this that I recommend revisiting your
file-transfer process instead of trying to outguess the
remote system's line-ending conventions.

--
Er*********@sun .com


Thanks a lot for the help. It worked. I did

p = strchr(line, '\n');
if (p != NULL)
*p = '\0';
q = strchr(line, '\r');
if (q != NULL)
*q = '\0';

This eliminated both "\n" and "\r". Thanks again.

May 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4389
by: Michael Palmer | last post by:
I'm reading xml from SQL Server 2K with VB.net using an XSD schema file and SQLXML 3.0. I have the below code working fine, but I'd like to change the code from reading the schema file from a directory to reading the schema as an embedded resource. Here's my current code, what would I need to change?? Dim strm As Stream Dim strmReader As StreamReader Dim cmd As New SqlXmlCommand("MyConnectionString") Dim xmlDoc As New XmlDocument
5
3978
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one file and store it on to another file using the following code. Could anyone please let me know how i could go about reading from multiple files. Thanks in advance. typedef struct {
6
5277
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
4
7852
by: Utahduck | last post by:
I do a lot of file processing and I usually run a little script I copy and paste to read directory information to see if a new file it there and then process the file if it is. So, I decided to wise up and make a stored procedure to automate a lot of that. The pivotal step in this is that i run a command that looks like: CREATE TABLE #DIR (FileName varchar(100)) DECLARE @Cmd varchar(1050)
3
3708
by: Paul Moore | last post by:
I'd like to write some scripts to analyze and manipulate my music files. The files themselves are in MP3 and FLAC format (mostly MP3, but FLAC where I ripped original CDs and wanted a lossless format). I've no idea what form of tags are used in the files (ID3v1, ID3v2, OGG, APE, ...) I just used whatever the program that set them up used. I'm completely confused by the various tag formats that exist - there seems to be little...
0
1607
by: jigsmshah | last post by:
I have a windows service developed in C#.I am reading the connection string from an ini file and also i am reading 3 image file from the bin directory. now the new requirement is that there will be multiple instances of the same windows service running. My concern is that will there be any problem when multiple instances will be reading those above mentioned file.I am not writing to any files in the service. If i try to use lock() when...
4
7270
by: Laharl | last post by:
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a bit of data about them, for the files) in the current directory and its subdirectories using system calls. Unfortunately, it isn't working quite right. It works fine in the current directory, but when it goes into subdirectories, the stat() function...
3
5943
by: stephen | last post by:
Hi, I have 5 excel files and they have multiple sheets. I have to read (say sheet 3) of each of the 5 excel files and consolidate them into one. what's the best way to achieve this. if someone can point me to an article or sample that will be real helpful. I am able to read a excel file and then do whatever I want but how to have 5 excel files open and then read and then consolidate. I am a bit confused here.
2
4103
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.