从csv文件读取和动态内存分配
我的代码有内存泄漏问题。我不知道我哪里出错了。下面是代码:我试图从csv文件中读取并存储特定列从csv文件读取和动态内存分配,c,dynamic-memory-allocation,C,Dynamic Memory Allocation,我的代码有内存泄漏问题。我不知道我哪里出错了。下面是代码:我试图从csv文件中读取并存储特定列 #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { FILE *result = fopen ("C:\\Users\\pa1rs\\Desktop\\local.csv", "w"); const char *text = "LOA,NAME,";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
FILE *result = fopen ("C:\\Users\\pa1rs\\Desktop\\local.csv", "w");
const char *text = "LOA,NAME,";
fprintf (result, "%s", text);
char *token;
char *endToken;
int lines = 0;
char ch; /* should check the result */
FILE *file = fopen ("C:\\Users\\pa1rs\\Desktop\\samplee.csv", "r");
char line[300];
if (file == NULL) {
perror ("Error opening the file");
} else {
while (!feof (file)) {
ch = fgetc (file);
if (ch == '\n') {
lines = lines + 1;
}
}
//printf(" no of lines existing in the file %d\n\n", lines);
}
fseek (file, 0, SEEK_SET);
while ((ch = fgetc (file)) != '\n') {
// we don't need the first line on sample.csv
// as it is just the description part
}
int s[lines - 1];
int j = 0;
char *N[lines - 1];
while (fgets (line, sizeof (line), file)) {
int i = 0;
token = line;
do {
endToken = strchr (token, ',');
if (endToken)
*endToken = '\0';
if (i == 3) {
s[j] = atoi (token);
}
if (i == 12) {
N[j] = (char *) malloc (strlen (token) * sizeof (char));
strcpy (N[j], token);
}
if (endToken)
token = endToken + 1;
i++;
} while (endToken);
j = j + 1;
}
//******************************************************unigue loa
int count = 0;
int g = 0;
int h = 0;
int LOA[lines - 1];
int dd = 0;
for (dd = 0; dd < lines - 1; dd++) {
LOA[dd] = 0;
}
for (g = 0; g < lines - 1; g++) {
for (h = 0; h < count; h++) {
if (s[g] == LOA[h])
break;
}
if (h == count) {
LOA[count] = s[g];
count++;
}
}
int xw = 0;
for (xw = 0; xw < count; xw++) {
//printf("%d \t",LOA[xw]);
}
//printf("LOA Array Length is: %d \n",count);
//********************************************************
////FOR UNIQUE NAMES ARRAY
//printf("No of unique names are %d",county);
//FOR UNIQUE CAUSES ARRAY
char *sa[9] =
{ "Monticello", "Valparaiso", "Crown Point", "Plymouth", "Goshen",
"Gary", "Hammond", "Laporte", "Angola" };
int countz = 0;
int gz = 0;
int hz = 0;
char *LOAz[lines - 1];
int zero2 = 0;
for (zero2 = 0; zero2 < lines - 1; zero2++) {
LOAz[zero2] = NULL;
}
for (gz = 0; gz < lines - 1; gz++) {
for (hz = 0; hz < countz; hz++) {
if (strcmp (N[gz], LOAz[hz]) == 0)
break;
}
if (hz == countz) {
LOAz[countz] = (char *) malloc (strlen (N[gz]) * sizeof (char));
strcpy (LOAz[countz], N[gz]);
countz++;
}
}
int nz = 0;
for (nz = 0; nz < countz; nz++) {
fprintf (result, "%s,", LOAz[nz]);
}
fprintf (result, "\n");
// printf("%d",countz);
//*****************************
int i = 0;
int jjj = 0;
int xxx = 0;
int ggg = 0;
int k = 0;
int kount[count][countz];
for (xxx = 0; xxx < count; xxx++) {
for (ggg = 0; ggg < countz; ggg++) {
kount[xxx][ggg] = 0;
}
}
for (i = 0; i < count; i++) {
for (k = 0; k < countz; k++) {
for (jjj = 0; jjj < lines - 1; jjj++) {
if (LOA[i] == s[jjj]) {
if (strcmp (LOAz[k], N[jjj]) == 0) {
kount[i][k]++;
}
}
}
}
}
int ig = 0;
int ik = 0;
for (ig = 0; ig < count; ig++) {
fprintf (result, "%d,%s", LOA[ig], sa[ig]);
for (ik = 0; ik < countz; ik++) {
fprintf (result, ",%d", kount[ig][ik]);
}
fprintf (result, "\n");
}
int rrr = 0;
free (N);
for (rrr = 0; rrr < lines - 1; rrr++) {
free (LOAz[rrr]);
}
//*****************************
//fclose(result);
fclose (file);
return 0;
}
#包括
#包括
#包括
int main()
{
文件*result=fopen(“C:\\Users\\pa1rs\\Desktop\\local.csv”,“w”);
const char*text=“LOA,NAME,”;
fprintf(结果,“%s”,文本);
字符*令牌;
char*endToken;
int行=0;
char ch;/*应检查结果*/
FILE*FILE=fopen(“C:\\Users\\pa1rs\\Desktop\\samplee.csv”,“r”);
字符行[300];
if(file==NULL){
perror(“打开文件时出错”);
}否则{
而(!feof(文件)){
ch=fgetc(文件);
如果(ch='\n'){
线条=线条+1;
}
}
//printf(“文件%d中存在的行数\n\n”,行);
}
fseek(文件,0,搜索集);
而((ch=fgetc(文件))!='\n'){
//我们不需要sample.csv上的第一行
//因为这只是描述部分
}
int s[行-1];
int j=0;
字符*N[行-1];
while(fgets(行、sizeof(行)、文件)){
int i=0;
令牌=行;
做{
endToken=strchr(token,,);
if(endToken)
*endToken='\0';
如果(i==3){
s[j]=atoi(令牌);
}
如果(i==12){
N[j]=(char*)malloc(strlen(token)*sizeof(char));
strcpy(N[j],令牌);
}
if(endToken)
token=endToken+1;
i++;
}while(endToken);
j=j+1;
}
//******************************************************洛阿大学
整数计数=0;
int g=0;
int h=0;
国际贷款[第1行];
int-dd=0;
对于(dd=0;dd
我在这里得到的行是13761,LOAz
是用数组大小的行-1=13761声明的,但我在这里得到的唯一的行只有49行,所以我正在为它重新分配内存,剩余的是未使用的,我想问题是从那里开始的。
请帮忙!提前感谢。代码中的一个问题是没有为字符串分配足够的内存。例如,在这些行中:
N[j] = (char*) malloc(strlen(token) * sizeof(char));
strcpy(N[j], token);
// ...
LOAz[countz] = (char*) malloc(strlen(N[gz]) * sizeof(char));
strcpy(LOAz[countz], N[gz]);
问题是strlen
返回字符串中非零符号的数量。但是,要存储字符串,还需要一个字节,以存储零终止字符,因此存储s
的缓冲区大小至少应为strlen(s)+1
另外,更好的编码风格是。sizeof(char)也非常无用。。。强制转换也是如此。要跟踪内存泄漏,您需要使用Valgrind()之类的内存检查器。使用
FILE*result=fopen
打开后,无法关闭result
。假设rrr
在LOAz[countz]
中循环countz
,则无法关闭文件是唯一的泄漏。另外,请整理您的申报单。通常,您应该在每个代码块的开头声明变量,而不是在每个代码块中进行临时喷洒。