c++用char数组存储字符串

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;

int cal_row() // calculate the row of textfile
{
    ifstream textfile;
    textfile.open("textfile.txt");
    textfile >> noskipws;
    int row = 1;
    char char1;
    while (textfile >> char1)
    {
        if (char1 == '\n')
        {
            ++row;
        }
    }
    return row;
}



char data()
{
    ifstream textfile;
    textfile.open("textfile.txt");
    textfile >> noskipws;
    char line, char2;
    int row, len = 1000, tlen;
    row = cal_row();
    tlen = row * len + 1;
    char data1[tlen];
    char *data2[tlen];
    len = 0;
    while (textfile >> char2) // store text as an array
    {
        if (char2 != '\n')
        {
            data1[len++] = char2;
        }
        else
        {
            data1[len++] = '\n';
        }
    }
    for (int i = 0; i <= tlen; i++) // uppercase to lowercase
    {
        if ((int)data1[i] <= 90 && (int)data1[i] >= 65)
        {
            data1[i] += 32;
        }
    }


    return *data1;
}



int main()
{
    int row = cal_row(), tlen = row * 1000 + 1, len1, len2, len3;
    len1 = len2 = len3 = 0;
    char text1[tlen], text2[row][20][51];
    *text1 = data();
    return 0;

我想知道如何修改代码,使得主函数中text1的数组可以和data函数中的返回值相同(即text1=分函数中的data1)


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;

int cal_row() // calculate the row of textfile
{
    ifstream textfile;
    textfile.open("textfile.txt");
    textfile >> noskipws;
    int row = 1;
    char char1;
    while (textfile >> char1)
    {
        if (char1 == '\n')
        {
            ++row;
        }
    }
    return row;
}



std::string data()
{
    ifstream textfile;
    textfile.open("textfile.txt");
    textfile >> noskipws;
    char line, char2;
    int row, len = 1000, tlen;
    row = cal_row();
    tlen = row * len + 1;
    //char data1[tlen];//两点错误:数组长度必须是常量;data1是局部变量,不能作为返回值,因为出了函数,data1就被释放了
    char* data1 = new char[tlen];
   // char* data2[tlen];
    len = 0;
    while (textfile >> char2) // store text as an array
    {
        if (char2 != '\n')
        {
            data1[len++] = char2;
        }
        else
        {
            data1[len++] = '\n';
        }
    }
    for (int i = 0; i <= tlen; i++) // uppercase to lowercase
    {
        if ((int)data1[i] <= 90 && (int)data1[i] >= 65)
        {
            data1[i] += 32;
        }
    }
    std::string retValue(data1);
    delete[] data1;

    return retValue;
}



int main()
{
    int row = cal_row(), tlen = row * 1000 + 1, len1, len2, len3;
    len1 = len2 = len3 = 0;
    //char text1[tlen], text2[row][20][51];
    char* text1 = new char[tlen];
    //*text1 = data();
    std::string value = data();
    strcpy(text1, value.c_str());
    //或者 memcpy(text1, value.c_str(), value.length() + 1);
    delete[] text1;
    return 0;

你写得太复杂了

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> lines;
    ifstream file("textfile.txt");
    string line;
    while (getline(file, line))
    {
        transform(line.begin(), line.end(), line.begin(), [](auto c) { return tolower(c); });
        lines.push_back(move(line));
    }
    for (const auto &l : lines)
        cout << l << '\n';
    return 0;
}