ChaosSpring 2022-03-25 18:13 采纳率: 100%
浏览 146
已结题

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)

  • 写回答

2条回答 默认 最新

  • stone_wangzx 2022-03-25 18:37
    关注
    
    #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;
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 修改了问题 3月25日
  • 创建了问题 3月25日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改