Skip to content

Instantly share code, notes, and snippets.

@frknbasaran
Last active September 28, 2017 18:57
Show Gist options
  • Save frknbasaran/fe5d8567653e74dbb464aee280377aab to your computer and use it in GitHub Desktop.
Save frknbasaran/fe5d8567653e74dbb464aee280377aab to your computer and use it in GitHub Desktop.
Read some text from txt and write it into another one w/ c++.
/*
Import dependencies
*/
// input output stream library
#include <iostream>
// string library for get line as a string from file
#include <string>
// included for streaming validate function
#include <sstream>
// file streaming library, using for (read by) and (write to) methods.
#include <fstream>
// include vector libraries for define string array
#include <vector>
// declare std namespace for using methods like 'method' instead of 'std::method' format
using namespace std;
// main application method
int main() {
// define source and target files as a string array
const char* const iFiles[] = {"Renewables.txt", "Basics.txt", "Precedence.txt", "Project_info.txt","U-renewables.txt","Request.txt"};
const char* const oFiles[] = {"Renewables_generated.txt", "Basics_generated.txt", "Precedence_generated.txt", "Project_info_generated.txt","U-renewables_generated.txt","Request_generated.txt"};
// calculate array sizes for loop iteration
const size_t lenIFiles = sizeof(iFiles) / sizeof(iFiles[0]);
const size_t lenOFiles = sizeof(oFiles) / sizeof(oFiles[0]);
// start loop
for (size_t i = 0; i < lenIFiles; ++i) {
// define sourcefile from source files array
ifstream sourceFile(iFiles[i]);
// define string for hold current line data
string currentLine;
// define target file from source files array
ofstream targetFile;
targetFile.open(oFiles[i]);
// loop while sourcefile line count not finished
while (getline(sourceFile, currentLine))
{
// validate string
istringstream iss(currentLine);
// write into target file line by line
targetFile << currentLine << endl;
}
}
// return 0; everythings worked well!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment