c++ - howto: Read input and store it in another file -
i want make program reads highest value 1 file , stores in another. i've read ifstream , ofstream how let ofstream store highest value instream in file? here have far:
#include <iostream> #include <fstream> #include <string> #include <algorithm> #include <iterator> #include <vector> using namespace std; struct csvwhitespace : ctype<char> { static const mask* make_table() { static vector<mask> v{classic_table(), classic_table() + table_size}; v[','] |= space; // comma classified whitespace return v.data(); } csvwhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {} } csvwhitespace; int main() { string line; ifstream myfile ("c:/users/username/desktop/log.csv"); ofstream myfile2 ("c:/users/username/desktop/log2.csv"); return 0; } auto v = vector<int>{}; myfile.imbue(locale{myfile.getloc(), &csvwhitespace}); copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v)); myfile2 << *max_element(begin(v), end(v)); } thanks in advance :)
you copy 1 file in other, without having worry format, treating them in binary mode. here example:
#include <stdio.h> #include <string.h> #define bufsize 1024 int main(int argc, char *argv[]) { file *ifp, *ofp; char buf[bufsize]; if (argc != 3) { fprintf(stderr, "usage: %s <soure-file> <target-file>\n", argv[0]); return 1; } if ((ifp = fopen(argv[1], "rb")) == null) { /* open source file. */ perror("fopen source-file"); return 1; } if ((ofp = fopen(argv[2], "wb")) == null) { /* open target file. */ perror("fopen target-file"); return 1; } while (fgets(buf, sizeof(buf), ifp) != null) { /* while don't reach end of source. */ /* read characters source file fill buffer. */ /* write characters read target file. */ fwrite(buf, sizeof(char), strlen(buf), ofp); } fclose(ifp); fclose(ofp); return 0; } which given example in ip, source. need specify cmd arguments desired files.
Comments
Post a Comment