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 8902563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:38:45+00:00 2026-06-15T01:38:45+00:00

I am having some problem with writing a function to extract strings from a

  • 0

I am having some problem with writing a function to extract strings from a file as part of a bigger program. Everything seems to be working fine, except when I use memset or bzero to erase the character arrays I have been using. I have been sitting with this problem for more than an hour and I keep getting seg faults whatever I do. I am getting this error for both bzero and memset. Please help me out.
I am attaching my code below. The statement “Come out of addfront” is printed but none of the “Done with all bzero” statements are printing. I get a segmentation fault at that point. Thank you

void extractFileData(FILE *fp , char clientName[])
{
    char tempFileName[50], tempFilePath[100], tempFileSize[50];
    struct stat fileDetails;

    while(fgets(tempFileName, sizeof(tempFileName), fp)!= NULL)
    {
        if((newLinePos = strchr(tempFileName, '\n')) != NULL)
        {
            *newLinePos = '\0';
        }

        strcat(tempFilePath, "SharedFiles/");
        strcat(tempFilePath, tempFileName);

        if(stat(tempFilePath, &fileDetails) < 0)
        {
            perror("Stat error");
            exit(1);
        }

        //Copy it into a string
        sprintf(tempFileSize, "%zu", fileDetails.st_size);
        printf("temp file size: %s\n", tempFileSize);

        //Add all these details to the file list by creating a new node
        addFront(tempFileName, tempFileSize, clientName);

        printf("Come out of addfront\n");

        memset(&tempFileName, 0, 45);
        printf("Done with all bzero\n");
        memset(&tempFileSize, 0, sizeof(tempFileSize));
        memset(&tempFilePath, 0, sizeof(tempFilePath));

        printf("Done with all bzero\n");
    }
}   

EDIT:

void addFront(char fileName[], char fileSize[], char clientName[])
{
    FILENODE* n;
    printf("Inside add front function\n");
    strcpy(n->fileName, fileName);
    printf("n->filename: %s\n", n->fileName);
    strcpy(n->fileSize, fileSize);
    printf("n->filesize: %s\n", n->fileSize);
    strcpy(n->ownerName, clientName);
    printf("n->ownername: %s\n", n->ownerName);
    myFileList.head = n;
    printf("Did it go past myfilelist head = n\n");
    myFileList.numOfNodes++;
    printf("num of nodes: %d\n", myFileList.numOfNodes);
}

I have added my code for the addFront function. It basically adds
the details to a struct myFileList which is basically an implementation
of a linked list. The FILENODE represents each entry in the list.

EDIT:

Adding the structs I am using

 struct fileNode
 {
      char fileName[50];
      char fileSize[50];
          char ownerName[25];
      struct fileNode* next;
 };

 struct fileList
 {
      struct fileNode* head;
      struct fileNode* tail;
       int numOfNodes;
 };

 typedef struct fileList FILELIST;
 typedef struct fileNode FILENODE;
  • 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-15T01:38:47+00:00Added an answer on June 15, 2026 at 1:38 am

    I don’t know why your program would crash there. But I can another error in the program. Fix the other error first, see if you still have problems.

    This is wrong:

    strcat(tempFilePath, "SharedFiles/");
    strcat(tempFilePath, tempFileName);
    

    The tempFilePath variable is uninitialized. This may coincidentally not crash, but you cannot rely on it not to crash. It may scribble on your stack.

    Do this instead:

    snprintf(tempFilePath, sizeof(tempFilePath), "SharedFiles/%s", tempFileName);
    

    Finally, there is no need to zero the arrays. The contents of the arrays are not used in the next loop iteration, so you might as well ignore them.

    void extractFileData(FILE *fp , char clientName[])
    {
        char tempFileName[50], tempFilePath[100], *newLinePos;
        struct stat fileDetails;
        while (fgets(tempFileName, sizeof(tempFileName), fp)) {
            if ((newLinePos = strchr(tempFileName, '\n')))
                *newLinePos = '\0';
            snprintf(tempFilePath, sizeof(tempFilePath),
                     "SharedFiles/%s", tempFileName);
            if (stat(tempFilePath, &fileDetails) < 0) {
                perror("Stat error");
                exit(1);
            }
            printf("temp file size: %zu\n", tempFileSize);
            addFront(tempFileName, tempFileSize, clientName);
        }
    }   
    

    The snprintf() function is really the number one choice for doing work like this in C. It’s easy to write code with snprintf() that “obviously won’t crash”, as opposed to code that “won’t obviously crash”.

    If your code still crashes, there is an error somewhere else.

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

Sidebar

Related Questions

I'm writing the following function to format some text from a file object 'item':
I'm having a problem designing part of my program (not writing it, for once!).
I am having some problems writing to a file in unicode inside my c
The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
Hi I'm having some problem with a PHP page: I'm writing a little CMS
I'm writing a recursive function in JS and having some trouble. Let's start with
I'm writing a small program to calculate a physics problem but I'm having problems
Having a problem with single quote/apostophes in some php I am writing to parse
I'm having some trouble writing a function to change a background image on a
I've been having some problems with a program that I've been writing and would

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.