473,890 Members | 1,350 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
15 15085
Richard Heathfield <in*****@invali d.invalid> writes:
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.

[...]

I agree.

On the other hand, it's quite possible that a solution other than
writing a C program would meet the OP's needs better.

If someone were to ask me how to copy a number of files (names given
as a list in a text file) to a single output file, writing a C program
would not be the first thing I'd think of. On a Unix-like system, a
one-line shell command would do it; on other systems, similar
solutions undoubtedly exist. (Perl springs to mind; a Perl program to
do this would be much shorter than a corresponding C program.)

It seems sensible to me to point this out *and* to help with a C
solution if that's what the OP really wants.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #11
Keith Thompson <ks***@mib.or g> wrote:
Richard Heathfield <in*****@invali d.invalid> writes:
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.
It wasn't, please read below.
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.[...]

I agree.

On the other hand, it's quite possible that a solution other than
writing a C program would meet the OP's needs better.


This was my thought, I apologize to the OP (and Richard) if my
response came out as rude or hostile.
If someone were to ask me how to copy a number of files (names given
as a list in a text file) to a single output file, writing a C program
would not be the first thing I'd think of. On a Unix-like system, a
one-line shell command would do it; on other systems, similar
solutions undoubtedly exist. (Perl springs to mind; a Perl program to
do this would be much shorter than a corresponding C program.)
Precisely. I have lost count of the number of times somebody asked me
for help and this dialog repeated itself:

He: "How can I do ZZ?"
Me: "Why do you want to do ZZ?"
He: "Because I need to do YY and that requires ZZ"
Me: "Why do you want to do YY?"
He: "Because I need to do XX and that requires YY"
Me: "Why do you want to do XX?"
He: "Because I need to do WW and that requires XX"
Me: "Have you considered doing AA instead of WW, this would solve
your problem directly."
He: "No, I didn't. Hmm, let me see, yes, that will do it."

I always try to understand the original problem that needs to be
solved, ignoring, at least temporarily, the new problems that pop up
once a particular solution was chosen. (Always annoying everybody
asking "What is the question behind your question?")

Once I was asked to help finish a C program that had to do some simple
data reduction on a few large text files. Each line on the files
contained a variable number of alphanumeric fields. The program had to
do some calculations on the values of the last two fields, depending
on the value of previous fields. The person doing this work had
written a very complex parser in C that failed to do job correctly.
A quick rendition of "How do I do this in C?" / "Why do you want to do
this in C" ensued, and the (correct) solution to the problem was an
8-line AWK script.
I (mis?)read the original post as a similar case, where a C solution,
while doable, would not have been an optimal one.
It seems sensible to me to point this out *and* to help with a C
solution if that's what the OP really wants.


I agree.
May 25 '06 #12
Keith Thompson said:
Richard Heathfield <in*****@invali d.invalid> writes:

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

I agree.

On the other hand, it's quite possible that a solution other than
writing a C program would meet the OP's needs better.

If someone were to ask me how to copy a number of files (names given
as a list in a text file) to a single output file, writing a C program
would not be the first thing I'd think of.


It wouldn't? It would sure as heck be the first thing I'd think of.
On a Unix-like system, a
one-line shell command would do it;
I might use that solution, but only if I were certain that the files would
be copied in the order I wanted. Finding that out might well take longer
than banging out a few lines of C.
on other systems, similar solutions undoubtedly exist.
Not *undoubtedly*, no. The C Standard does /not/ impose that requirement.
And in any case, why have a "similar" solution for each platform when you
can have an /identical/ solution for each platform.
(Perl springs to mind; a Perl program to
do this would be much shorter than a corresponding C program.)
But by the time you've learned Perl and written a Perl interpreter for, or
ported one to, your target system, it might just have been quicker to bang
out a few lines of C.
It seems sensible to me to point this out *and* to help with a C
solution if that's what the OP really wants.


Shame to go only half-way, then, really.

--
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 25 '06 #13
Richard Heathfield <in*****@invali d.invalid> writes:
Keith Thompson said:
Richard Heathfield <in*****@invali d.invalid> writes:

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

[...]

I agree.

On the other hand, it's quite possible that a solution other than
writing a C program would meet the OP's needs better.

If someone were to ask me how to copy a number of files (names given
as a list in a text file) to a single output file, writing a C program
would not be the first thing I'd think of.


It wouldn't? It would sure as heck be the first thing I'd think of.
On a Unix-like system, a
one-line shell command would do it;


I might use that solution, but only if I were certain that the files would
be copied in the order I wanted. Finding that out might well take longer
than banging out a few lines of C.


The problem statement says that the file names are listed in a text
file, presumably in the desired order. If I use the list file, the
files are going to be copied in the right order.
on other systems, similar solutions undoubtedly exist.


Not *undoubtedly*, no. The C Standard does /not/ impose that requirement.
And in any case, why have a "similar" solution for each platform when you
can have an /identical/ solution for each platform.


It's entirely possible that this was a one-time task. But of course
if you need a portable solution, and the only tool you can assume is a
C compiler on each target system, then a C program could be the best
(or only) solution.

I'm not arguing that a C program *isn't* the best solution, only that
it *might* not be.

For me, I guarantee you that if I wanted to accomplish this task on a
Unix-like system, I could do it much faster and more reliably using
something other than C (but my solution would probably use tools that
happen to be implemented in C).
(Perl springs to mind; a Perl program to
do this would be much shorter than a corresponding C program.)


But by the time you've learned Perl and written a Perl interpreter for, or
ported one to, your target system, it might just have been quicker to bang
out a few lines of C.


Maybe yes, maybe no. By the time *I've* learned Perl and made sure
there's a Perl interpreter on my target system -- oh, wait, I already
have. But that's just an example, and I don't claim that it's
applicable to anyone other than me.
It seems sensible to me to point this out *and* to help with a C
solution if that's what the OP really wants.


Shame to go only half-way, then, really.


Fortunately, we didn't (this newsgroup being a collaborative effort),
and the OP probably got at least as much good advice as he was looking
for.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 25 '06 #14
Keith Thompson said:
...the OP probably got at least as much good advice as he was looking
for.


Eventually, yes.

--
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 25 '06 #15
On Thu, 25 May 2006 07:51:14 +0000,
Richard Heathfield <in*****@invali d.invalid> wrote
in Msg. <nr************ ********@bt.com >
Keith Thompson said:

If someone were to ask me how to copy a number of files (names given
as a list in a text file) to a single output file, writing a C program
would not be the first thing I'd think of.


It wouldn't? It would sure as heck be the first thing I'd think of.


At least it *should* be when the question is asked on clc.

Otherwise it's

xargs cat \{\} >> resulting_big_f ile < list.txt

(which should work on any reasonably set-up Windows system, too).

And by the way, even for a one-off on a system without GNU tools I'd do
it in C. I've become sick of Perl.

robert
May 26 '06 #16

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

Similar topics

1
4391
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
3981
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
5283
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
7855
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
3712
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
1609
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
7282
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
5948
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
4107
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
9980
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10468
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...
0
9641
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
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
5855
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
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.