Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:23:40+00:00 2026-06-14T11:23:40+00:00

I am trying to separate my input data file into two output files based

  • 0

I am trying to separate my input data file into two output files based on a label.Here is my code below.The below code works only for less number of records but it goes into Segmentation fault for more no of. rows.

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:23:42+00:00Added an answer on June 14, 2026 at 11:23 am

    I see a few problems in your code, first strlen returns the length of string not including the null byte so you don’t need -1 (that’s why it probably doesn’t match any of the strcmp‘s)

    memcpy(mystr, &token1[0], strlen(token1));
    mystr[strlen(token1)] = '\0';
    

    And here I think you should close 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
    

    Note: you should make sure that token1 doesn’t overflow mystr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do 2 output data from mysql to a csv file.
I am trying to separate a few things in here. I have a program
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I am trying to create two separate sessions- one for if the user is
I am trying to create two separate sessions- one for if the user is
So I'm trying to combine two Google Maps API tutorials into one map. This
I want to be able to separate the birthday from the mysql data into
I am trying to synchronize data between seperate Views in MVVM. Here is the
I have an upload form with two file upload fields. I am trying to
I am trying to create a single dynamic webpage with two separate forms: Sign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.