Preprocessing a file to remove all \n or endline characters from file and print results to new file
Mar 26, 2019 at 10:00pm UTC
The following code removes the endline character from all lines of the doc_word_test.txt file and prints correctly to the screen (or command prompt). However, only a few words from the middle of the doc_word_test.txt file gets printed to the new_doc_file.txt. How do I get the code to correctly remove the endline character from the original file and print to the new file without any of the newline characters?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <Windows.h>
#include <math.h>
#include "pch.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cctype>
#include <iomanip>
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
using namespace std;
int main()
{
vector<vector<string> > my2Dvector;
vector<vector<string> > temp;
vector<vector<string> > v;
ifstream file("doc_word_test.txt" );
string line, str_count, str;
int vector_counter, move;
move = 1;
ofstream file_;
while (getline(file, line))
{
istringstream iss(line);
string value;
vector<string> my1Dvector;
file_.open("new_doc_file.txt" );
while (iss >> value)
{
for (int i = 0; i < value.length(); i++) {
if (value[i] == '\n' ) {
value[i] = ' ' ;
}
}
file_ << value << ' ' ;
cout << value << ' ' ;
}
file_.close();
}
return 0;
}
Mar 26, 2019 at 10:14pm UTC
1 2 3 4 5 6 7 8
ifstream in("doc_word_test.txt" );
ofstream out("new_doc_file.txt" );
string line;
while (getline(in, line))
{
out << line;
}
Last edited on Mar 26, 2019 at 10:14pm UTC
Mar 26, 2019 at 10:48pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
using it = istream_iterator<char >;
using ot = ostream_iterator<char >;
int main()
{
ifstream in( "doc_word_test.txt" );
ofstream out( "new_doc_file.txt" );
replace_copy( it{ in >> noskipws }, it{}, ot{ out }, '\n' , ' ' );
}
Mar 26, 2019 at 10:58pm UTC
Thanks a bunch...this worked. :=))
Topic archived. No new replies allowed.