// Assignment7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream infile;
int index = 0;
string singleWord;
infile.open("text.txt");
while (!infile.eof()) //to figure out size of array
{
infile >> singleWord;
index++;
}
string* strings = new string[index];
infile.close(); //to restart the file at the first word
infile.open("text.txt");
int count = 0;
while (!infile.eof()) //if not at end of file, continue reading numbers
{
infile >> strings[count];
cout << strings[count] << " ";
count++;
}
infile.close(); //close file
ofstream outfile;
outfile.open("textfixed.txt");
for (int i = 0; i < index; i++) {
outfile << strings[i] << " ";
}
outfile.close();
delete[] strings;
strings = NULL;
return 0;
}
My program reads a textfile that has 6 sentences setup as such:
Sentence 1
Sentence 2
Sentence 3
Sentence 4
Sentence 5
Sentence 6
I'm learning how to play with strings, and I want to concatenate the 6 sentences so that in a new file(that my program would write to, called newtext.txt) would have it setup like: Sentence 1. Sentence 2. Sentence 3. Sentence 4. Sentence 5. Sentence 6. So for example, if the textfile contained something like:
I like to play soccer.
I enjoy eating apples.
I am 18 years old.
I watch baseball sometimes.
Cooking is a hobby of mine.
My birthday is in July.
My program would take the above^ and in a new file write it out as:
I like to play soccer. I enjoy eating apples. I am 18 years old. I watch baseball sometimes. Cooking is a hobby of mine. My birthday is in July.
I'm just not quite sure how I go about concatenating each word in order to setup as such. If anyone could point me into the right direction, that'd be appreciated. Thanks.
UPDATED OP WITH NEW CODE
I don't get any output from the newly updated code, it's supposed to cout() the new paragraph to console but when I run it the CMD is just black