转:C++读取txt数据并存入数组

 
#include <iostream>
#include <fstream>
 
using namespace std;
 
const int ROW = 27;
const int VOL = 30;
 
int main(){
    double d[ROW][VOL];
    ifstream in("D:\\data.txt");//打开文件
    //读数据。。
    for(int i = 0; i < ROW; ++i){
        for(int j = 0; j < VOL; ++j){
            in >> d[i][j];
        }
    }
    in.close();//关闭文件
    //输出结果
    for(int i = 0; i < ROW; ++i){
        for(int j = 0; j < VOL; ++j){
            cout<<d[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 ------------------------------

问题:路径必须是直接在D盘下,不能改到D盘下的其他文件夹中,不知为何。。。

转载于:https://www.cnblogs.com/fulivia/p/3479091.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下步骤读取 BMP 文件的数据段并将其存入 uint8_t 数组中: 1. 打开 BMP 文件并验证文件格式和文件头。 2. 定位到数据段的起始位置。BMP 文件的数据段起始于文件头的偏移量指定的位置。 3. 读取数据段的大小,并根据大小分配内存以存储数据。 4. 使用文件指针或相关函数从数据段中读取数据并存储到 uint8_t 数组中。 5. 关闭 BMP 文件。 以下是一个示例代码,用于展示如何实现这些步骤: ```c #include <stdio.h> #include <stdint.h> #define BMP_HEADER_SIZE 54 int main() { FILE* file = fopen("example.bmp", "rb"); if (file == NULL) { perror("Cannot open file"); return 1; } // 读取文件头 uint8_t header[BMP_HEADER_SIZE]; fread(header, sizeof(uint8_t), BMP_HEADER_SIZE, file); // 验证文件格式 if (header[0] != 'B' || header[1] != 'M') { printf("Invalid BMP format\n"); fclose(file); return 1; } // 定位数据段 uint32_t data_offset = *(uint32_t*)&header[10]; // 移动文件指针到数据段起始位置 fseek(file, data_offset, SEEK_SET); // 读取数据段大小 uint32_t data_size = *(uint32_t*)&header[34]; // 分配内存来存储数据 uint8_t* data = (uint8_t*)malloc(data_size); if (data == NULL) { printf("Memory allocation failed\n"); fclose(file); return 1; } // 读取数据段并存储到数组 fread(data, sizeof(uint8_t), data_size, file); // 数据处理... // 关闭文件和释放内存 fclose(file); free(data); return 0; } ``` 请确保将文件名替换为您要读取的 BMP 文件的实际路径。此示例代码仅提供了读取数据段的基本框架,您可以根据需要进行进一步的数据处理或错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值