I'm trying to add code in the beginning of approximately 3000 files. The codes are going to be the same on every file. However all of the 3000 files contain texts, therefore I need to create a program that loops through all the files and adds the codes just in the beginning of the file.
Do you know the most efficient way to approach this?
Write a perl script to do it. It's really easy.
If you're running linux you can probably even write a bash script that would do it.
| Stubru Freak wrote: |
| Write a perl script to do it. It's really easy. |
Can this thing be done in C/C++ as i dont know perl
plz help
| umeshtangnu wrote: |
| Stubru Freak wrote: | | Write a perl script to do it. It's really easy. |
Can this thing be done in C/C++ as i dont know perl
plz help |
Of course. But I don't know how.
Are you using windows or Linux?
| MrBlueSky wrote: |
| Are you using windows or Linux? |
Does that really matter if you're using C or C++?
| umeshtangnu wrote: |
Can this thing be done in C/C++ as i dont know perl
plz help |
Yes, anything that can be done by your operating system can be done via C or C++, directly or indirectly.
There are several ways you can solve this problem.
1.) Use RAM.
Open file "file.dat" and read it into a stringstream with whatever you want prefixed at the start of the stringstream, then just write the stringstream back out into "file.dat":
| Code: |
// As always, all code is untested
void add_file_prefix(string const& filename, string const& prefix)
{
fstream file(filename.c_str(), ios_base::in | ios_base::out | ios_base::binary);
if (file.is_open())
{
// Setup prefix
stringstream data;
data << prefix;
// Read file into memory (after prefix)
data << file;
// Write file (including prefix)
file.seekp(0);
file << data;
}
} |
2.) Use the hard drive.
Create a temporary file, write the prefix, then copy the original file into the temporary. Then delete the original, and copy the temporary file to the original.
| Code: |
void add_file_prefix(string const& filename, string const& prefix)
{
// Get temporary file name
array<char, L_tempnam + 1> temporary_file_name;
tmpnam(temporary_file_name.data());
temporary_file_name[L_tempname] = '\0'; // Just to make sure it's null-terminated
ifstream in_file(filename.c_str(), ios_base::binary);
ofstream temp_file(temporary_file_name.data(), ios_base::binary);
if (in_file.is_open() && temp_file.is_open())
{
// Write prefix
temp_file << prefix;
// Write rest of file
temp_file << in_file;
// Close everything
in_file.close();
temp_file.close();
// Copy temp file over to the original file
remove(filename.c_str());
rename(temporary_file_name.data(), filename.c_str());
}
} |
3.) Hybrid approach.
Use RAM for files less than a certain size, and the hard drive for large files. Or use RAM until the memory use exceeds a certain amount, then use the hard drive as a buffer.
4.) Devious approaches.
Reading and writing at the same time - not easy, and not really portable, so i won't cover it. OS functions to change the file data right on the disk, etc. etc. Not really worth the grief, frankly.
My opinion: if the files are small (on the order of maybe 10 MB or less) use the RAM method. Even better, use the RAM method with threads. You'll need Boost for this:
| Code: |
deque<string> filenames;
string prefix;
mutex the_mutex;
void add_file_prefix(string const& filename, string const& prefix); // either of the versions above
void do_prefixing()
{
bool done = false;
do
{
mutex::scoped_lock lock(the_mutex);
if (!filenames.empty())
{
string filename = filenames.front();
filenames.pop_front();
lock.unlock()
add_file_prefix(filename, prefix);
}
else
{
done = true;
}
} while (!done);
}
int main()
{
// Fill in the list of filenames as you please - no need for the mutex
int const N = 10; // Number of threads you want
thread_group threads;
for (int i = 0; i < N; ++i)
threads.create(do_prefixing);
threads.join_all()
return 0;
} |
| Indi wrote: |
| MrBlueSky wrote: | | Are you using windows or Linux? |
Does that really matter if you're using C or C++?
|
Yes. The right tool for the right job. On Linux you do something like this:
| Code: |
sed -i '1a <Some text here>' *
|
A waste of time to write a program for this when you only want to do this once or twice.
| MrBlueSky wrote: |
| Indi wrote: | | MrBlueSky wrote: | | Are you using windows or Linux? |
Does that really matter if you're using C or C++?
|
Yes. The right tool for the right job. On Linux you do something like this:
| Code: |
sed -i '1a <Some text here>' *
|
A waste of time to write a program for this when you only want to do this once or twice. |
You didn't answer my question. i said: "Does that really matter if you're using C or C++?" He specifically asked for a C++ solution, then you asked for his OS. The right tool for the right job is a good axiom. The right answer for the right question is a good one, too.
| Indi wrote: |
You didn't answer my question. i said: "Does that really matter if you're using C or C++?" He specifically asked for a C++ solution, then you asked for his OS. The right tool for the right job is a good axiom. The right answer for the right question is a good one, too. |
Heh, to reply on BlueSky's behalf, in the software industry most of the time what the client asks for isn't actually what the client wants 
| AftershockVibe wrote: |
| Indi wrote: |
You didn't answer my question. i said: "Does that really matter if you're using C or C++?" He specifically asked for a C++ solution, then you asked for his OS. The right tool for the right job is a good axiom. The right answer for the right question is a good one, too. |
Heh, to reply on BlueSky's behalf, in the software industry most of the time what the client asks for isn't actually what the client wants  |
i can't speak for the software industry, but in my industry, if the client asks me for a coil car and i give them a hydraulic press, i won't be business very long. i can't imagine that the software industry does not operate on similar principles.
Granted, if a client asks for something to be done in a certain way and i think i know a better way, i will tell them about it. But if they still ask for it to be done their way, i will do it their way. That's exactly what happened here. You suggested the bash script, and it was rejected. Instead, a C++ solution was requested. What logic is there in suggesting the bash script again?
Then again, given the quality of most commercial software, maybe what you say is true and the software industry really does work that way.
| Indi wrote: |
| You suggested the bash script, and it was rejected. Instead, a C++ solution was requested. |
Where in the start post is C/C++ mentioned
Where in this topic is does anybody mention a bash-script
Anway, I don't think anybody is waiting for this kind of semantic nitpicking.
| MrBlueSky wrote: |
| Where in the start post is C/C++ mentioned |
It is not in the start post, it is in the post right before yours (and the post before that).
| MrBlueSky wrote: |
| Where in this topic is does anybody mention a bash-script |
Second or third post. Right before he asks for it in C or C++.