You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将无空格TXT文件存入char数组?程序输出乱码求解决

问题分析与解决思路

核心问题:文件指针已到末尾,无法二次读取

调用fileLength函数后,文件流fin的指针已经移动到文件末尾,此时fin处于EOF状态,再调用storeInArray根本读不到任何内容,txtCharacters数组里全是未初始化的垃圾数据,输出自然是乱码。

修复方法:重置文件指针

在调用storeInArray前,清除EOF错误标志并将读指针移回文件开头:

fin.clear(); // 清除EOF等错误状态
fin.seekg(0, ios::beg); // 将读指针定位到文件起始位置

其他潜在问题及修复

  1. 数组越界访问
    main函数中的循环条件i<=sizeArray错误,txtCharacters数组的有效索引范围是0到sizeArray-1,应改为:

    for(int i=0; i<sizeArray; i++)
    {
        cout << txtCharacters[i];
    }
    
  2. >>运算符的读取局限性
    fin >> nextIn会自动跳过所有空白字符(换行、制表符等),哪怕文件里只有无空格字符,换行符也会被忽略,导致统计的长度和实际不符。如果要读取所有字符,改用fin.get(nextIn),同时用fin.good()判断读取状态更可靠:

    // 修改fileLength函数的读取逻辑
    int fileLength(fstream& fin)
    {
        char nextIn;
        int count = 0;
        fin.get(nextIn);
        while(fin.good())
        {
            count++;
            fin.get(nextIn);
        }
        return count;
    }
    
    // 修改storeInArray函数的读取逻辑
    void storeInArray(fstream& fin, char arr[], int length)
    {
        int i = 0;
        char nextIn;
        fin.get(nextIn);
        while(fin.good() && i < length )
        {
            arr[i] = nextIn;
            i++;
            fin.get(nextIn);
        }
    }
    
  3. C++不支持变长数组(VLA)
    char txtCharacters[sizeArray]是C风格变长数组,不属于C++标准特性,兼容性差。建议改用std::vector<char>:

    #include <vector> // 需包含头文件
    
    // 替换原数组定义
    std::vector<char> txtCharacters(sizeArray);
    // 调整storeInArray的参数为vector引用
    void storeInArray(fstream& fin, std::vector<char>& arr)
    {
        int i = 0;
        char nextIn;
        fin.get(nextIn);
        while(fin.good() && i < arr.size())
        {
            arr[i] = nextIn;
            i++;
            fin.get(nextIn);
        }
    }
    
  4. 冗余的storeCharacter数组
    fileLength函数中的storeCharacter数组完全多余,统计长度不需要存储字符,直接计数即可,简化后函数更高效。

简化修复后的完整示例

#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>

using namespace std;

int fileLength(fstream& fin)
{
    char nextIn;
    int count = 0;
    fin.get(nextIn);
    while(fin.good())
    {
        count++;
        fin.get(nextIn);
    }
    return count;
}

void storeInArray(fstream& fin, vector<char>& arr)
{
    int i = 0;
    char nextIn;
    fin.get(nextIn);
    while(fin.good() && i < arr.size())
    {
        arr[i] = nextIn;
        i++;
        fin.get(nextIn);
    }
}

int main()
{
    fstream fin;
    int sizeArray = 0;
    
    fin.open("message.txt");
    if(fin.fail())
    {
        cout << "输入文件打开失败(文件名错误或其他问题)" << endl;
        exit(0);
    }
    
    sizeArray = fileLength(fin);
    cout << sizeArray << endl; 
    
    fin.clear();
    fin.seekg(0, ios::beg);
    
    vector<char> txtCharacters(sizeArray);
    storeInArray(fin, txtCharacters);
    
    for(char c : txtCharacters)
    {
        cout << c;
    }
    cout << endl;
    
    fin.close();

    return 0;
}

内容的提问来源于stack exchange,提问作者baconeater4521

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:25:57