 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
coldblood Guest
|
Posted: Wed Jan 21, 2004 9:07 am Post subject: Needed Tricky Program... |
|
|
HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
Please help.
Best regards
Michal 'coldblood' Przybylowicz
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
v Guest
|
Posted: Wed Jan 21, 2004 4:36 pm Post subject: Re: Needed Tricky Program... |
|
|
hi,
well i havent got one for you, but c++ is definitely not the best choice for
the job.
go for python, perl or tcl to handle such stuff.
bye
vlado
"coldblood" <coldblood (AT) op (DOT) pl> schrieb im Newsbeitrag
news:buk5he$po5$1 (AT) inews (DOT) gazeta.pl...
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
code_wrong Guest
|
Posted: Wed Jan 21, 2004 7:47 pm Post subject: Re: Needed Tricky Program... |
|
|
"coldblood" <coldblood (AT) op (DOT) pl> wrote
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
Please help.
|
you can use the system call to write the filenames to a text file
then read each filename into an append file routine
eg for a windows system
system("dir /b *.cpp > cpplist.txt");
so now you have a textfile that you can read the
filenames from.
run through the list opening for append
and add your newline.
close the file
Don't do what I did and open files for writing as you will
destroy the contents of the files ... ouch
cw
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Jan 21, 2004 7:48 pm Post subject: Re: Needed Tricky Program... |
|
|
coldblood wrote:
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
snip |
You can't do this in standard C++ because it has no means of
scanning directories.
Since you're working on Windows, this command should do the
trick:
for %f in (*.hpp *.cpp) do echo. >>"%f"
(Putting a "." rather than a space after "echo" forces it to
print its arguments rather than controlling command printing.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
code_wrong Guest
|
Posted: Wed Jan 21, 2004 7:53 pm Post subject: Re: Needed Tricky Program... |
|
|
"coldblood" <coldblood (AT) op (DOT) pl> wrote
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
|
here's an idea
use
system("dir *.cpp /b > cpplist.txt");
to write a bare listing of cpp files to a text file
then read the lines in the file into
"A file open for append operation"
thereby opening each file listed for append
append what you want
then close the file
repeat for each file
repeat for each file type
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Wed Jan 21, 2004 7:56 pm Post subject: Re: Needed Tricky Program... |
|
|
Hi,
| Quote: | I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
|
Really not at all c++ related. A tiny shell script should
be able to do that. Get an implementation of the bash, then
the following should help (untested!).
for i in *.hpp; do
echo "" >>$i
done;
for i in *.cpp; do
echo "" >>$i
done;
Works also on the command line of the bash.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mike Y. Guest
|
Posted: Thu Jan 22, 2004 8:54 am Post subject: Re: Needed Tricky Program... |
|
|
On 21 Jan 2004 14:48:59 -0500, Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com>
wrote:
| Quote: |
Since you're working on Windows, this command should do the
trick:
for %f in (*.hpp *.cpp) do echo. >>"%f"
(Putting a "." rather than a space after "echo" forces it to
print its arguments rather than controlling command printing.)
|
Building on the dos batch command solution, you can append files together
using the "copy" command:
copy source.cpp+newline.txt newsource.cpp
source.cpp is your source file
newline.txt is a text file containing a new line
newsource.cpp is the combined file
I don't think you can specify the same filename for the source file and
target file. Well, this is a partial solution anyway. Good luck.
--Mike Y.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Thu Jan 22, 2004 8:57 am Post subject: Re: Needed Tricky Program... |
|
|
"coldblood" <coldblood (AT) op (DOT) pl> wrote
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
|
This can be easily done in a small shell/cmd script that updates
all these files using a small script/C/"cat" program for adding a couple
of chars to a file ("cat" utility is used to concatenate files). You need
to write the changed file with a different name/suffix, and then rename
the original and the new files. There is nothing tricky in this program.
A more complicated option is to use an editor with a macro that
appends a line to the end, saves a file and quits. Microsoft Word
and vim can easily do this for you.
Your problem will be the changed dates of these files. Any make facility,
including MSVC, will be confused with this and would recompile everything.
| Quote: | For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
|
Did you try to find a version of DJGPP without this "feature"?
If it's stuck only on the main source files, you can make a small preprocessor
that adds a "#line" stmt at the start and an empty line at the end,
and prints everything on the standard output.
A better solution would be to use a different C preprocessor that
puts a new line after #include'ing a file, and to feed the preprocessed
source to your DJGPP.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Thu Jan 22, 2004 9:17 am Post subject: Re: Needed Tricky Program... |
|
|
Hi!
coldblood wrote:
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
|
Apart from the other solutions given, look into boost::filesystem to get
the files in the current directory.
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Jan 23, 2004 10:02 am Post subject: Re: Needed Tricky Program... |
|
|
"coldblood" <coldblood (AT) op (DOT) pl> wrote
| Quote: | I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then I finally compile them
under DJGPP - but there during compiling I have an error message that:
"error: no new line at the end of file", then I must manualy add one
character at the end of ever file and the complie process continues -
this sucks. That's why I need such tricky program.
|
As others have pointed out, this is really a job for a simple shell
script. If you really want to do it in C++, however, the Boost
Filesystem Library has a directory_iterator which looks like it would do
exactly what you want. Apply Boost's filter iterator adaptor to it, and
you've got the filenames you want (and only those you want).
Finally, of course, when opening the files, be very careful about the
modes. Just opening the file for writing will truncate it, even if you
don't specify the truncate flag. For a maximum of compatibility with
older (sometimes broken) versions of iostream, I'd open with ios::in and
ios::out, then seek to the end, but normally, ios::out | ios::app should
do the trick (and the seek will not be necessary).
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
coldblood Guest
|
Posted: Fri Jan 23, 2004 10:12 am Post subject: Re: Needed Tricky Program... |
|
|
Użytkownik "Michael Tiomkin" <tmk (AT) netvision (DOT) net.il> napisał w wiadomości
news:ef72c6cb.0401211404.31d96e0a (AT) posting (DOT) google.com...
| Quote: | "coldblood" <coldblood (AT) op (DOT) pl> wrote
HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
This can be easily done in a small shell/cmd script that updates
all these files using a small script/C/"cat" program for adding a couple
of chars to a file ("cat" utility is used to concatenate files). You need
to write the changed file with a different name/suffix, and then rename
the original and the new files. There is nothing tricky in this program.
A more complicated option is to use an editor with a macro that
appends a line to the end, saves a file and quits. Microsoft Word
and vim can easily do this for you.
Your problem will be the changed dates of these files. Any make
facility,
including MSVC, will be confused with this and would recompile everything.
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
Did you try to find a version of DJGPP without this "feature"?
|
No, I haven't look for it yet ( someone told me that this
is some kind of new standard for source files, but if so
why MSVC do not put a requaired character at the end
of each file ? ).
| Quote: | If it's stuck only on the main source files, you can make a small
preprocessor
that adds a "#line" stmt at the start and an empty line at the end,
and prints everything on the standard output.
|
Its stuck on all source files that have been changed in MSVC editor :(
regards
coldblood
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Wilson Guest
|
Posted: Fri Jan 23, 2004 9:05 pm Post subject: Re: Needed Tricky Program... |
|
|
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
|
Vanity compels me to suggest you can use the C++/STL mappings in the
recls project - http://recls.org/ - or even the WinSTL
(http://winstl.org/ for windows) or UNIXSTL (http://unixstl.org/ for
UNIX) STL-like sequence components upon which it is built.
Cheers
Matthew Wilson
STLSoft moderator
(http://www.stlsoft.org)
Contributing editor, C/C++ Users Journal
(www.synesis.com.au/articles.html#columns)
"But if less is more, think how much more more will be!" -- Dr Frazier
Crane
-------------------------------------------------------------------------------
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri Jan 23, 2004 9:11 pm Post subject: New-line at end of file (was Needed Tricky Program...) |
|
|
coldblood wrote:
<snip>
| Quote: | ( someone told me that this
is some kind of new standard for source files, but if so
why MSVC do not put a requaired character at the end
of each file ? ).
snip |
I think all C and C++ standards have required that source files
consist of complete lines of source. In a stream-oriented file-
system this means there is a new-line character or characters at
the end of the file. (In a record-oriented file-system it is
impossible to have incomplete lines.)
There is probably an explanation of the reason(s) for this in the
rationale for ANSI C (C89) but I don't have that. One reason
could be that a pre-processor might join an incomplete line at
the end of an included file with the next line of the file that
included it, changing the interpretation of the program.
However, there is no requirement on an implementation to reject
files with incomplete lines (the result is "undefined behaviour",
so anythng could happen).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Jan 27, 2004 11:03 am Post subject: Re: Needed Tricky Program... |
|
|
"coldblood" <coldblood (AT) op (DOT) pl> wrote
| Quote: | HI.
I need a small program that do the following things:
1) Reads how many files ( with extensions: *.hpp and *.cpp )
are in the same dir that this tricky program resides.
2) Opens every single file and adds ONE character at
the end of it ( it could be only one space for ex. )
For those who are interested why I need such program
read this:
I use MSVC++ 6.0 to write my C++ programs, then
I finally compile them under DJGPP - but there
during compiling I have an error message that:
"error: no new line at the end of file", then I must
manualy add one character at the end of ever file
and the complie process continues - this sucks.
That's why I need such tricky program.
|
Here is fixline.bat:
@echo off
for %%x in (*.hpp) do call addline.bat %%x
for %%x in (*.cpp) do call addline.bat %%x
Here is addline.bat:
@echo //>>%1
Copy these files into your source directory. Before you compile, switch to
the proper directory and call fixline.bat.
Note: If you run this multiple times on the same source file, you will add
a new comment line to the end of the file each time. While this probably
wouldn't interfere with most compiles, if you ran it thousands of times
it could eventually start swamping the size of the actual code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|