>p) { p=可读字(p); //cout,c++,C++" /> >p) { p=可读字(p); //cout,c++,C++" />

我需要写一个c++;有这些目标的计划 一个从文本文件读取数据的C++程序。假设文本文件包含一个 关于任何主题的段落。您的程序要求用户输入不带扩展名的文件名。现在,一个 用户定义函数(名称:ReadWordByWord())逐字读取所有数据并存储在 具有根据数据动态增长的字符类型数组。 最后,声明一个用户定义的函数(名称:SaveInReverse()),该函数将此文本存储到 文本文件(名称由用户输入)的顺序与单词的顺序相反,例如,最后一个单词将在开始时存储, 然后是原始文件的第二个最后一个字、第三个最后一个字等。 这就是我到目前为止所做的。。。这里我没有使用delete命令,如果使用该命令,将导致一个错误—堆错误。我怎样才能先做到这一点?那么有什么建议可以改进这个计划呢 #include<iostream> #include<string> #include<fstream> using namespace std; char* readWordByWord(char * old) { int coun = 0; for (int i = 0; old[i] != '\0'; i++) // to find the length of word { coun++; } char *newArr = new char[coun + 1]; strcpy(newArr, old); //delete[]old; // this is where i am putting delete command to delete the previous i.e old array and then return the new one return newArr; } int size = 100; int main() { fstream fin; string forCopy[1000]; int index = 0; fin.open("file.txt"); char *p = new char[size]; while (fin >> p) { p = readWordByWord(p); //cout << p<<endl; forCopy[index++] = p; /*for (int i = 0; p[i] != '\0'; i++) { }*/ } for (int i = index - 1; i > 0; i--) cout << forCopy[i] << " "; delete[]p; p = NULL; fin.close(); return 0; } #包括 #包括 #包括 使用名称空间std; char*readWordByWord(char*old) { int CONN=0; for(int i=0;old[i]!='\0';i++)//查找单词的长度 { coun++; } char*newArr=新字符[coun+1]; strcpy(newArr,old); //delete[]old;//在这里,我将使用delete命令删除以前的数组,即旧数组,然后返回新数组 返回newArr; } int size=100; int main() { 流鳍; 字符串forCopy[1000]; int指数=0; 打开(“file.txt”); char*p=新字符[大小]; 而(fin>>p) { p=可读字(p); //cout

我需要写一个c++;有这些目标的计划 一个从文本文件读取数据的C++程序。假设文本文件包含一个 关于任何主题的段落。您的程序要求用户输入不带扩展名的文件名。现在,一个 用户定义函数(名称:ReadWordByWord())逐字读取所有数据并存储在 具有根据数据动态增长的字符类型数组。 最后,声明一个用户定义的函数(名称:SaveInReverse()),该函数将此文本存储到 文本文件(名称由用户输入)的顺序与单词的顺序相反,例如,最后一个单词将在开始时存储, 然后是原始文件的第二个最后一个字、第三个最后一个字等。 这就是我到目前为止所做的。。。这里我没有使用delete命令,如果使用该命令,将导致一个错误—堆错误。我怎样才能先做到这一点?那么有什么建议可以改进这个计划呢 #include<iostream> #include<string> #include<fstream> using namespace std; char* readWordByWord(char * old) { int coun = 0; for (int i = 0; old[i] != '\0'; i++) // to find the length of word { coun++; } char *newArr = new char[coun + 1]; strcpy(newArr, old); //delete[]old; // this is where i am putting delete command to delete the previous i.e old array and then return the new one return newArr; } int size = 100; int main() { fstream fin; string forCopy[1000]; int index = 0; fin.open("file.txt"); char *p = new char[size]; while (fin >> p) { p = readWordByWord(p); //cout << p<<endl; forCopy[index++] = p; /*for (int i = 0; p[i] != '\0'; i++) { }*/ } for (int i = index - 1; i > 0; i--) cout << forCopy[i] << " "; delete[]p; p = NULL; fin.close(); return 0; } #包括 #包括 #包括 使用名称空间std; char*readWordByWord(char*old) { int CONN=0; for(int i=0;old[i]!='\0';i++)//查找单词的长度 { coun++; } char*newArr=新字符[coun+1]; strcpy(newArr,old); //delete[]old;//在这里,我将使用delete命令删除以前的数组,即旧数组,然后返回新数组 返回newArr; } int size=100; int main() { 流鳍; 字符串forCopy[1000]; int指数=0; 打开(“file.txt”); char*p=新字符[大小]; 而(fin>>p) { p=可读字(p); //cout,c++,C++,我已经从头开始写了(不能包括换行,因为只需要一段): #包括 #包括 #包括 void reverse(char*begin,char*end){//反转单词 焦炭温度; while(开始

我已经从头开始写了(不能包括换行,因为只需要一段):

#包括
#包括
#包括
void reverse(char*begin,char*end){//反转单词
焦炭温度;
while(开始<结束){
温度=*开始;
*开始+=*结束;
*结束--=温度;
}
}
void reverseParagraph(char*arg){//反转段落中的每个单词
char*word_begin=arg;
char*temp=arg;
while(*temp){
temp++;
如果(*temp=='\0')
反向(单词开始,临时-1);
否则,如果(*温度=“”){
反向(单词开始,临时-1);
单词_begin=temp+1;
}
}
反向(arg,temp-1);
}
int main(){
std::ifstream文件(“file.txt”);
std::string fileData=“”;
while(getline(file,fileData));//计算内存分配的字母数
size_t len=fileData.length();
char*str=新字符[len+1];
strcpy(str,fileData.c_str());
reverseParagraph(str);//反转整个字符指针

std::cout为什么要使用数组而不是容器类,比如
vector
#include <iostream>
#include <fstream>
#include <string>

void reverse(char *begin, char *end) { // reverses a word
    char temp;
    while (begin < end) {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}

void reverseParagraph(char *arg) { // reverses each word of the paragraph
    char *word_begin = arg;
    char *temp = arg;

    while (*temp) {
        temp++;
        if (*temp == '\0')
            reverse(word_begin, temp - 1);

        else if (*temp == ' ') {
            reverse(word_begin, temp - 1);
            word_begin = temp + 1;
        }
    }

    reverse(arg, temp - 1);
}

int main() {
    std::ifstream file("file.txt");
    std::string fileData = "";

    while(getline(file, fileData)); // counting the number of letters for memory allocation
    size_t len = fileData.length();

    char *str = new char[len + 1];
    strcpy(str, fileData.c_str());

    reverseParagraph(str); // reverse the entire character pointer

    std::cout << str << std::endl; // displays for testing

    std::ofstream fileOut("out.txt");
    fileOut << str << std::endl; // saving the output into another file
    fileOut.close();

    delete[] str;
    file.close();

    return 0;
}