如何用vc 读取文件中每一行的某个数据
1个回答
展开全部
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define PATHNAME "E:/1.txt"
#define COUNT 2064
int getMark(char*, char[]);
int getAttr(char* , float[], int*);
int checkData(char*);
int main(int argc, char* argv[])
{
int* pMark = NULL;
float* pAttr[COUNT];
char* buff = NULL;
char ch[9];
float attr[24];
int i = 0;
int icnt;
FILE* pf = NULL;
if (!(pf = fopen(PATHNAME, "r+"))) {
printf("error!!!");
return 0;
}
pMark = new int[COUNT];
if (!pMark) {
printf("error!!!");
return 0;
}
memset(pMark, 0, COUNT*sizeof(int));
buff = new char[256];
if (!buff) {
printf("error!!!");
return 0;
}
memset(buff, 0, 256*sizeof(char));
while (!feof(pf) && i < COUNT) { // 1.判断文件是否到了结尾 2.判断读取行数是否超过COUNT
pAttr[i] = new float[24];
if (!pAttr[i]) {
printf("error!!!");
return 0;
}
memset(pAttr[i], 0, 24*sizeof(float));
i++;
fgets(buff, 256, pf); // 读取256字节数据或者到一行结束的数据
if (checkData(buff) == -1)
continue;
memset(ch, 0, 9*sizeof(char));
getMark(buff, ch); // 得到每行的第一个数据里
*(pMark + i - 1) = atoi(ch); // 写到第一个数组里
memset(attr, 0, 24*sizeof(float));
getAttr(buff, attr, &icnt); // 得到每行的其它数据后,存在数组attr里
memcpy(pAttr[i - 1], attr, (icnt+1)*sizeof(float)); // 将数组attr中的数据拷贝到第二个数组里
}
/*
1.txt:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
1, 0.020147, 0.005431, 0.001509, 0.000059, 0.000003, 0.000797, 0.111808
2, 0.026044, 0.001199, 0.001091, 0.000081, 0.000003, 0.000794, 0.111806
2, 0.090909, 0.008505, 0.003210, 0.001535, 0.000004, 0.000909, 0.111562
3, 0.354464, 0.198575, 0.028486, 0.024844, 0.000663, 0.012116, 0.113298
3, 0.552498, 0.364998, 0.240247, 0.210239, 0.047230, 0.130700, 0.010317
*(pMark+0) = 1;
*(pMark+1) = 1;
*(pMark+2) = 2;
*(pMark+3) = 2;
*(pMark+4) = 3;
*(pMark+5) = 3;
...
*(pAttr[0]+0) = 0.056183
*(pAttr[0]+1) = 0.015906
...
*(pAttr[1]+0) = 0.020147
*(pAttr[1]+1) = 0.005431
...
*/
delete [] pMark;
delete [] buff;
for (int j = 0; j < i; j++) {
delete [] pAttr[j];
}
fclose(pf);
return 0;
}
int checkData(char* buff)
{
/*
此处为数据格式check,过程省略。
现在默认你的数据都是合法的,如果有非法数据此程序可能会出错,此时必须对数据格式进行check
buff:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
*/
return 0;
}
int getMark(char* buff, char ch[])
{
int iLength = strlen(buff);
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
memcpy(ch, buff, i);
break;
}
}
return 0;
}
int getAttr(char* buff, float attr[], int* icnt)
{
char ch[9];
int iLength = strlen(buff);
*icnt = -1;
int istart = 0;
int iend = 0;
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
iend = i;
(*icnt)++;
if (*icnt < 1) {
istart = iend;
continue;
}
memset(ch, 0, 9*sizeof(char));
memcpy(ch, buff + istart + 2, 8);
attr[*icnt - 1] = (float)atof(ch);
istart = iend;
}
}
return 0;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define PATHNAME "E:/1.txt"
#define COUNT 2064
int getMark(char*, char[]);
int getAttr(char* , float[], int*);
int checkData(char*);
int main(int argc, char* argv[])
{
int* pMark = NULL;
float* pAttr[COUNT];
char* buff = NULL;
char ch[9];
float attr[24];
int i = 0;
int icnt;
FILE* pf = NULL;
if (!(pf = fopen(PATHNAME, "r+"))) {
printf("error!!!");
return 0;
}
pMark = new int[COUNT];
if (!pMark) {
printf("error!!!");
return 0;
}
memset(pMark, 0, COUNT*sizeof(int));
buff = new char[256];
if (!buff) {
printf("error!!!");
return 0;
}
memset(buff, 0, 256*sizeof(char));
while (!feof(pf) && i < COUNT) { // 1.判断文件是否到了结尾 2.判断读取行数是否超过COUNT
pAttr[i] = new float[24];
if (!pAttr[i]) {
printf("error!!!");
return 0;
}
memset(pAttr[i], 0, 24*sizeof(float));
i++;
fgets(buff, 256, pf); // 读取256字节数据或者到一行结束的数据
if (checkData(buff) == -1)
continue;
memset(ch, 0, 9*sizeof(char));
getMark(buff, ch); // 得到每行的第一个数据里
*(pMark + i - 1) = atoi(ch); // 写到第一个数组里
memset(attr, 0, 24*sizeof(float));
getAttr(buff, attr, &icnt); // 得到每行的其它数据后,存在数组attr里
memcpy(pAttr[i - 1], attr, (icnt+1)*sizeof(float)); // 将数组attr中的数据拷贝到第二个数组里
}
/*
1.txt:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
1, 0.020147, 0.005431, 0.001509, 0.000059, 0.000003, 0.000797, 0.111808
2, 0.026044, 0.001199, 0.001091, 0.000081, 0.000003, 0.000794, 0.111806
2, 0.090909, 0.008505, 0.003210, 0.001535, 0.000004, 0.000909, 0.111562
3, 0.354464, 0.198575, 0.028486, 0.024844, 0.000663, 0.012116, 0.113298
3, 0.552498, 0.364998, 0.240247, 0.210239, 0.047230, 0.130700, 0.010317
*(pMark+0) = 1;
*(pMark+1) = 1;
*(pMark+2) = 2;
*(pMark+3) = 2;
*(pMark+4) = 3;
*(pMark+5) = 3;
...
*(pAttr[0]+0) = 0.056183
*(pAttr[0]+1) = 0.015906
...
*(pAttr[1]+0) = 0.020147
*(pAttr[1]+1) = 0.005431
...
*/
delete [] pMark;
delete [] buff;
for (int j = 0; j < i; j++) {
delete [] pAttr[j];
}
fclose(pf);
return 0;
}
int checkData(char* buff)
{
/*
此处为数据格式check,过程省略。
现在默认你的数据都是合法的,如果有非法数据此程序可能会出错,此时必须对数据格式进行check
buff:
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
*/
return 0;
}
int getMark(char* buff, char ch[])
{
int iLength = strlen(buff);
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
memcpy(ch, buff, i);
break;
}
}
return 0;
}
int getAttr(char* buff, float attr[], int* icnt)
{
char ch[9];
int iLength = strlen(buff);
*icnt = -1;
int istart = 0;
int iend = 0;
for (int i = 0; i < iLength; i++) {
if (buff[i] == ',') {
iend = i;
(*icnt)++;
if (*icnt < 1) {
istart = iend;
continue;
}
memset(ch, 0, 9*sizeof(char));
memcpy(ch, buff + istart + 2, 8);
attr[*icnt - 1] = (float)atof(ch);
istart = iend;
}
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询