Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将一个文件拆分为两个文件以获得400k记录_C_File_Split_Data Cleaning - Fatal编程技术网

C 如何将一个文件拆分为两个文件以获得400k记录

C 如何将一个文件拆分为两个文件以获得400k记录,c,file,split,data-cleaning,C,File,Split,Data Cleaning,我正试图根据标签将输入数据文件分为两个输出文件。下面是我的代码。下面的代码仅适用于较少的记录,但对于更多的记录,它会出现分段错误。排 #include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) { FILE *fp,*fp1,*fp2,*fp3; char *filename,line[80],line1[80]

我正试图根据标签将输入数据文件分为两个输出文件。下面是我的代码。下面的代码仅适用于较少的记录,但对于更多的记录,它会出现分段错误。排

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char *argv[])
{
        FILE *fp,*fp1,*fp2,*fp3;
        char *filename,line[80],line1[80];
        char *token,*token1,mystr[10];

        filename=(char *)argv[1];
        fp=fopen(filename,"r");

        if(fp ==NULL) //Checking whether the command line argument was correctly or not.
        printf("There is no such file in the directory.\n");

        if(remove("sales_ok_fraud.txt") != 0) //Checking for file existence.
        perror("Error in deleting the file.\n");
        else
        printf("The existing cleansed data file is successfully deleted.\n");

        if(remove("sales_unknwn.txt") != 0) //Checking for file existence.
        perror("Error in deleting the file.\n");
        else
        printf("The existing cleansed data file is successfully deleted.\n");

        while(fgets(line,80,fp)!=NULL) //Reading each line from file to calculate the file size.
        {
                strcpy(line1,line);
                token = strtok(line,",");
                token = strtok(NULL,",");
                token = strtok(NULL,",");
                token = strtok(NULL,",");
                token = strtok(NULL,",");
                token = strtok(NULL,",");
                token1 = strtok(token,"\n");
                memcpy(mystr,&token1[0],strlen(token1)-1);
                mystr[strlen(token1)-1] = '\0';


                if( strcmp(mystr,"ok") == 0 )
                {
                        fp1=fopen("sales_ok_fraud.txt","a");//Opening the file in append mode.
    fprintf(fp1,"%s",line1);//Writing into the file.
                        fclose(fp2);//Closing the file.

                        //printf("Inside ok - %s\n",mystr);
                }
                else if( strcmp(mystr,"fraud") == 0)
                {
                        fp2=fopen("sales_ok_fraud.txt","a");//Opening the file in append mode.
                        fprintf(fp2,"%s",line1);//Writing into the file.
                        fclose(fp2);//Closing the file.
                        //printf("Inside fraud - %s\n",mystr);
                }
                else
                {
                        fp3=fopen("sales_unknwn.txt","a");//Opening the file in append mode.
                        fprintf(fp3,"%s",line1);//Writing into the file.
                        fclose(fp3);//Closing the file.
                        //printf("This is unknown record.\n");
                }
        }

        fclose(fp);

        return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
文件*fp、*fp1、*fp2、*fp3;
char*文件名,第[80]行,第1[80]行;
char*token,*token1,mystr[10];
filename=(char*)argv[1];
fp=fopen(文件名,“r”);
if(fp==NULL)//检查命令行参数是否正确。
printf(“目录中没有这样的文件。\n”);
if(remove(“sales\u ok\u fraud.txt”)!=0)//检查文件是否存在。
perror(“删除文件时出错。\n”);
其他的
printf(“已成功删除已清除的现有数据文件。\n”);
if(remove(“sales_unknwn.txt”)!=0)//检查文件是否存在。
perror(“删除文件时出错。\n”);
其他的
printf(“已成功删除已清除的现有数据文件。\n”);
while(fgets(line,80,fp)!=NULL)//从文件中读取每一行以计算文件大小。
{
strcpy(第1行,第2行);
令牌=strtok(行,“,”);
令牌=strtok(空,“”,“”);
令牌=strtok(空,“”,“”);
令牌=strtok(空,“”,“”);
令牌=strtok(空,“”,“”);
令牌=strtok(空,“”,“”);
令牌1=strtok(令牌“\n”);
memcpy(mystr和token1[0],strlen(token1)-1);
mystr[strlen(token1)-1]='\0';
如果(strcmp(mystr,“ok”)==0)
{
fp1=fopen(“sales_ok_fraud.txt”,“a”);//以追加模式打开文件。
fprintf(fp1,“%s”,第1行);//写入文件。
fclose(fp2);//关闭文件。
//printf(“内部正常-%s\n”,mystr);
}
否则如果(strcmp(mystr,“欺诈”)==0)
{
fp2=fopen(“sales_ok_fraud.txt”,“a”);//以追加模式打开文件。
fprintf(fp2,“%s”,第1行);//写入文件。
fclose(fp2);//关闭文件。
//printf(“内部欺诈-%s\n”,mystr);
}
其他的
{
fp3=fopen(“sales_unknwn.txt”,“a”);//以追加模式打开文件。
fprintf(fp3,“%s”,第1行);//写入文件。
fclose(fp3);//关闭文件。
//printf(“这是未知记录。\n”);
}
}
fclose(fp);
返回0;
}

我在您的代码中发现了一些问题,首先
strlen
返回不包含空字节的字符串长度,因此您不需要
-1
(这就是它可能与任何
strcmp
不匹配的原因)

在这里,我认为您应该关闭
fp1

fp1=fopen("sales_ok_fraud.txt","a");  //you open f1
fprintf(fp1,"%s",line1);              //you write
fclose(fp2);//Closing the file.       //you close fp2

注意:您应该确保
token1
不会溢出
mystr

是的,这就是错误所在。错误关闭文件指针。:)
fp1=fopen("sales_ok_fraud.txt","a");  //you open f1
fprintf(fp1,"%s",line1);              //you write
fclose(fp2);//Closing the file.       //you close fp2