Hi guys,

It's been forever since I did any programming, and came upon a project where a simple program would speed things up for me tremendously...Basically, I have a folder with thousands of text files in it and I want to add a block of text to the beginning of each of those files...I've quickly poked through some tutorials and can open streams to a text file, but I can't figure out how to just add a block of text to the beginning of the file...additionally, I'd like the program to be able to just go through each file in the folder in sequence and change it from a .txt file to a .html file...any tips or pointers you could give me would be greatly appreciated...thanks!

-Eric

Open the .txt file for input
Open the .html file for output
Output the header
Read the .txt file
Write it to the output file
Close both

You can't just add text to the beginning of a file. You have to completely rewrite the file to do that. First, open a new file, write the new text to it, then copy the old file to the end of the new file. Next, delete the original file then rename the new file to the same name as the old file.

If the file is small enough you can just read the entire file into memory, then rewite it with the new informatin followed by the olf information.


How to get a list of all the files in a folder will depend on the operating system. MS-Windows -- call FindFirstFile() and FindNextFile(). *nix call opendir() and readdir(). After that, call rename() for each file. You could just rename the files as they are retrieved from the previous two function calls if you want to do it that way.

[edit]Also as Walt ^^^ said :)

I have windows, so I'll be using the FindFirstFile() and FindNextFile() functions, but I just want to make sure - are they in the fstream header file or some other one? Thanks for the tips guys! I'll keep at this and come back when I've run into another wall on this...

They are part of the win32 api functions. Just include windows.h. google for those functions and you will find lots of examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.