本文主要是介绍C读取CSV文件,通过某种条件,按照特定行和同一列进行读取,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
读取CSV文件,按照特定行和列进行读取
1、source
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <wchar.h>
#include <locale.h>#define MAX_LINE_LENGTH 1000
#define MAX_COLUMNS 50// 函数声明
int findTargetColumn(wchar_t* line);
void getColumnValue(wchar_t* line, int target_column, wchar_t* columnValue);void process_block(FILE* file) {int is_data_block_1 = 0; // 是否是区分列为1的数据块int is_data_block_0 = 0; // 是否是区分列为0的数据块int target_column = -1; // 带●的列号int columnNums = 0;wchar_t buffer[MAX_LINE_LENGTH];wchar_t saved_column[MAX_COLUMNS]; // 保存当前列号wchar_t columnValueDif0[MAX_LINE_LENGTH]; //根据列下标确定的区分0的值// 读取一行数据while (fgetws(buffer, sizeof(buffer) / sizeof(buffer[0]), file) != NULL) {// 去掉换行符wchar_t* newline = wcschr(buffer, L'\n');if (newline) {*newline = L'\0';}// 如果遇到#开头的行,跳过if (buffer[0] == L'#') {continue;}// 如果遇到空行,说明数据块结束,重置标志if (buffer[0] == L'\0') {is_data_block_1 = 0;target_column = -1;continue;}// 判断是否是区分列为1的数据块int col_number = 0;wchar_t* token = buffer;// 找第二列while (*token && col_number < 1) {if (*token == L',') {col_number++;}token++;}// 如果第二列为 "1"if (col_number == 1 && ((*token) == L'1')) {is_data_block_1 = 1;}if (is_data_block_1 == 1) {while (*token) {if (*token == L',') {col_number++;}else if (*token == L'●') {//如果有带符号●的wchar_t* symbolPos = wcschr(token,',');if (symbolPos != NULL) {*symbolPos = L'\0';wchar_t* temp = token + 1;*symbolPos = L','; // 还原原始数据//一共三种方式,还有wcsncpy:wchar_t* temp2 = symbolPos;wchar_t data1[MAX_LINE_LENGTH];wchar_t data2[MAX_LINE_LENGTH];memcpy(data1, token+1, temp2 - token);int len = (temp2 - token) / sizeof(wchar_t);data1[len] = '\0';wcsncpy(data2, token + 1, temp2 - token);data2[len] = '\0';}else if (symbolPos == NULL) { //最后一列,后面没有逗号wchar_t* temp = token + 1;}saved_column[columnNums++] = col_number;}token++;}}//如果区分块是0的场合if (col_number == 1 && ((*token) == L'0')) {is_data_block_0 = 1;}if (is_data_block_0 == 1) {for (int i = 0; i < columnNums; i++) {int columnNum = saved_column[i];getColumnValue(buffer, columnNum, columnValueDif0);}}// 重置标志is_data_block_1 = 0;target_column = -1;}
}// 辅助函数:寻找带●的那一列的列号
int findTargetColumn(wchar_t* line) {int col_number = 0;wchar_t* token = line;while (*token) {if (*token == L',') {col_number++;}else if (*token == L'●') {return col_number;}token++;}return -1; // 未找到
}// 辅助函数:根据列号获取对应的列值
void getColumnValue(wchar_t* line, int target_column, wchar_t* columnValue) {wchar_t* token = line;int col_number = 0;// 遍历列,找到对应的列值while (*token) {if (*token == L',') {col_number++;}else if (col_number == target_column) {// 将一行的值拆分成数组wchar_t* start = token;while (*token && *token != L',') {token++;}// 拷贝列值到 columnValuewcsncpy(columnValue, start, token - start);columnValue[token - start] = L'\0'; // 手动添加 null 终止符return;}token++;}
}int main() {setlocale(LC_ALL, ""); // 设置本地化,以便支持宽字符FILE* file = fopen("D:\\vs2019C++practise\\YoutubeC\\source\\Test\\CF.csv", "r, ccs=UTF-8"); // 以UTF-8编码方式打开文件if (file == NULL) {perror("Error opening file");return 1;}// 逐行读取文件并处理数据块process_block(file);fclose(file);return 0;
}
2、可以用的方法
#include <stdio.h>
#include <wchar.h>
#include <locale.h>#define MAX_LINE_LENGTH 1000// 辅助函数:寻找带●的那一列的列号
int findTargetColumn(wchar_t *line) {int col_number = 0;wchar_t *token = line;while (*token) {if (*token == L',') {col_number++;} else if (*token == L'●') {return col_number;}token++;}return -1; // 未找到
}void process_block(FILE *file) {int is_data_block_1 = 0; // 是否是区分列
这篇关于C读取CSV文件,通过某种条件,按照特定行和同一列进行读取的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!