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

  • SEARCH
  • Home
  • 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 8524123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:36:05+00:00 2026-06-11T07:36:05+00:00

I am trying to get each line from a file and doing some string

  • 0

I am trying to get each line from a file and doing some string operations. But my piece of code throws seg fault. I tested the same thing for one line as a different program and it works fine. But when I read from a file and do string manipulation operations it throws me segmentation fault.

Variable declarations:

char *pch3,str1[100],str2[100]


 pch3 = strtok(line3,",");
                        while(pch3!=NULL)
                        {
                                if(strcmp(pch3,"?") == 0)
                                {
                                       strcat(str1,"0");
                                       strcat(str1,",");
                                }
                                else
                                {
                                       strcat(str1,pch3);
                                       strcat(str1,",");
                                }
                                pch3 = strtok(NULL,",");
                        }
                        strlen1=strlen(str1);
                        memcpy(str2,str1,strlen1-1);
                        fp2=fopen("breast-cancer-wisconsin-miscellaneous-cleansed.data","a");
                        fprintf(fp2,"%s\n",str2);
                        fclose(fp2);
  • 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-11T07:36:07+00:00Added an answer on June 11, 2026 at 7:36 am

    You do not initialize str1 and str2, which means when you use strcat it tries to find the end of the previous string, but that can be anywhere in str1, even outside str1 if str1 does not contain a zero.

    Change the declaration to this and it should work better:

    char *pch3, str1[100] = "", str2[100] = "";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write out a regex to get each insert line from
I'm trying to get each element on each line in a text file so
I'm trying to store each line from a file into an arraylist and then
I was trying to write a piece of Java code to read each line
I'm trying to get Python to a read line from a .txt file and
Trying to get two checkbox controls to work with each other. Pseudo-code example: If
I am trying to get the number of comments from each of the posts
I'm trying to read a line of text from a text file and put
I'm trying to just get rid of duplicate consecutive words from a text file,
I am trying to replace text in a text file by reading each line,

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.