-3
// 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;
}
我的程序讀取具有6句設置爲這樣的文本文件:級聯線從一個文本文件到一個段並把它輸出到另一個文件
語句1
句子2
句3
句子4
句子5
句子6
我正在學習如何與琴絃,我要連接的6句,這樣在一個新的文件(我的程序會寫不出來,叫newtext.txt中)將有它設置如:句子1.句子2.句子3.句子4.句子5.句子6.例如,如果文本文件包含如下內容:
我喜歡踢足球。
我喜歡吃蘋果。
我今年18歲。
有時看棒球。
烹飪是我的愛好。
我的生日是在七月。
我的程序將採取上述^,並在新文件中寫出來的:
我喜歡踢足球。我喜歡吃蘋果。我18歲。我有時看棒球。烹飪是我的愛好。我的生日是在七月。
我只是不太確定如何連接每個單詞以便設置。如果任何人都可以指引我走向正確的方向,那會很感激。謝謝。
從新的代碼我沒有得到來自新近更新的代碼的輸出,它應該清點()的新段落控制檯,但
的第一步是要弄清楚',而(infile.eof() )'[總是一個錯誤](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。您可以先修復代碼,以便正確讀取文件。 –
如果你使用字符串,你可以使用'+'來連接。 –
@WasiAhmad我想弄清楚如何使用數組中的單詞,它是每個索引,我不得不連接? – Xor