Adding text and lines to the beginning of a file
Asked Answered
A

1

6

I'd like to be able to add lines to the beginning of a file.

This program I am writing will take information from a user, and prep it to write to a file. That file, then, will be a diff that was already generated, and what is being added to the beginning is descriptors and tags that make it compatible with Debian's DEP3 Patch tagging system.

This needs to be cross-platform, so it needs to work in GNU C++ (Linux) and Microsoft C++ (and whatever Mac comes with)

(Related Threads elsewhere: http://ubuntuforums.org/showthread.php?t=2006605)

Adder answered 19/6, 2012 at 19:44 Comment(5)
possible duplicate of In C++, what is the proper way to insert a line at the beginning of a text file?Foliar
Does look like a duplicate, but that other question doesn't have very good answers.Cresting
@JonathanWakely you know a better language that's cross platform?Adder
For text processing? Sure, python.Cresting
@JonathanWakely I'll ask how to do this in Python too (in a different question), since I'd rather use python for linux platforms. I chose C++ because Linux, Mac, and Windows all can run C++ (no need to maintain two programs). Setting up python in Windows is... tricky might be the word... in order to get it correctly runAdder
P
12

See trent.josephsen's answer:

You can't insert data at the start of a file on disk. You need to read the entire file into memory, insert data at the beginning, and write the entire thing back to disk. (This isn't the only way, but given the file isn't too large, it's probably the best.)

You can achieve such by using std::ifstream for the input file and std::ofstream for the output file. Afterwards you can use std::remove and std::rename to replace your old file:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

int main(){
    std::ofstream outputFile("outputFileName");
    std::ifstream inputFile("inputFileName");

    outputFile << "Write your lines...\n";
    outputFile << "just as you would do to std::cout ...\n";

    outputFile << inputFile.rdbuf();

    inputFile.close();
    outputFile.close();

    std::remove("inputFileName");
    std::rename("outputFileName","inputFileName");
    
    return 0;
}

Another approach which doesn't use remove or rename uses a std::stringstream:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int main(){
    const std::string fileName = "outputFileName";
    std::fstream processedFile(fileName.c_str());
    std::stringstream fileData;

    fileData << "First line\n";
    fileData << "second line\n";

    fileData << processedFile.rdbuf();
    processedFile.close();

    processedFile.open(fileName.c_str(), std::fstream::out | std::fstream::trunc); 
    processedFile << fileData.rdbuf();

    return 0;
}
Panpipe answered 19/6, 2012 at 19:52 Comment(5)
The while loop can be replaced with outputFile << inputFile.rdbuf();Rebbeccarebe
After writing to the other file, he can remove the original file, and rename the other file with the original name. May want to call close on the fstreams before doing that though.Rebbeccarebe
Instead of using a vector<string> why not just use a stringstream?Cresting
Thanks for the suggestions, I guess this wasn't one of at my best days :/… (std::vector instead of std::stringstream? Really? Damn, I believe I was asleep when I wrote that).Panpipe
Yes, that looks much better now :)Cresting

© 2022 - 2024 — McMap. All rights reserved.