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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:25:20+00:00 2026-06-17T13:25:20+00:00

I have a code that scans an entire directory for .wor files, and extract

  • 0

I have a code that scans an entire directory for .wor files, and extract info from them. When I run the file on a directory with 5 .wor files, it works perfectly. But when i try to run it on a directory with 70+ .wor files, it gives me an “Memery fault(coredump” error.

So i tried to debug the code with gdb, and here is what i got:

Program received signal SIGSEGV, Segmentation fault.
0x7af9fcbc in _sbrk () from /usr/lib/libc.2
(gdb) bt
#0  0x7af9fcbc in _sbrk () from /usr/lib/libc.2
#1  0x7af9db7c in _sigfillset () from /usr/lib/libc.2
#2  0x7af9bfe8 in __thread_callback_np () from /usr/lib/libc.2
#3  0x7afa0e64 in malloc () from /usr/lib/libc.2
#4  0x7b001160 in _findbuf () from /usr/lib/libc.2
#5  0x7afff7f4 in _filbuf () from /usr/lib/libc.2
#6  0x7afff404 in __fgets_unlocked () from /usr/lib/libc.2
#7  0x7afff5fc in fgets () from /usr/lib/libc.2
#8  0x2c14 in main (argc=1, argv=0x7dff11b4) at book2.c:99

I looked at line 99 and i have the command: while( fgets(buff, 100,fs) != NULL ), I do not know c well enough to find where the error is, any help here would be really appreciated, thanks in advance!

Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>


int main(int argc, char **argv) {

char directory[100];
char buff[100];
char delims[] = " :=";
char *result = NULL;
char *customer;
char *device;
char *testprog;
char *software;
char *dutboardid;
char *corlbox;
char *testerid;
char *temp;
char *buffer;
int i=0;
DIR * FD;
struct dirent* in_file;
int c=0;


printf ("Enter directory:");                                   
    scanf ("%s",directory);


    FILE * ft = fopen ( "workorderlist.csv", "w" ) ;    /* Open file to write to*/
    if ( ft == NULL )
    {
           puts ( "Cannot open target file" ) ;
           exit( 1 ) ;
    }


fprintf (ft, "Work Order,Customer,Device,Test Program,Software,DUT_board_id,Corl box, Tempurature, Test Station\n");



/* Open Directory*/

if (NULL == (FD = opendir (directory))) 
    {

        puts ("Cannot open directory");

    return 1;
    }


    while ((in_file = readdir(FD))) 
    {

    if (!strcmp (in_file->d_name, "."))
        continue;
    if (!strcmp (in_file->d_name, ".."))    
        continue;



    /* Open files to read from  */

buffer = (char*) malloc (200);
sprintf(buffer, "%s/%s", directory, in_file->d_name);


size_t len = strlen(buffer);
if (len >= 4 && memcmp(buffer + len - 4, ".wor", 4) == 0)   /* checks if file ends with .wor */
{

FILE * fs = fopen(buffer, "r");       /* open file to read */

customer = "0";
device = "0";
testprog = "0";
software = "0";
dutboardid = "0";
corlbox = "0";
testerid = "0";
temp = "0";

    if (fs == NULL)
    {
        puts ("Cannot open source file");

        return 1;
    }

    /* Scanning each file for targeted words: */


while( fgets(buff, 100,fs) != NULL )      
{   



 result = strtok( buff, delims );          
    while(result != NULL){   


            if((strcmp(result,"Customer")==0)){ 
        result = strtok(NULL,delims);  
        customer = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(customer, result);
        for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
        customer[i] = ' ';
        }

            if((strcmp(result,"name")==0)){ 
        result = strtok(NULL,delims);  
        customer = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(customer, result);
        for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
        customer[i] = ' ';
        }


            if(strcmp(result,"device")==0){ 
        result = strtok(NULL,delims);  
        device = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(device, result);
        for(i=0;i<strlen(device)+1;i++){ if(device[i] == '\n') break; } 
        device[i] = ' ';
        }


            if(strcmp(result,"test_prog")==0 || strcmp(result, "testprog")==0){ 
        result = strtok(NULL,delims);  
        testprog = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(testprog, result);
        for(i=0;i<strlen(testprog)+1;i++){ if(testprog[i] == '\n') break; } 
        testprog[i] = ' ';
        }


            if(strcmp(result,"Rev")==0 || strcmp(result,"use")==0){ 
        result = strtok(NULL,delims);  
        software = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(software, result);
        for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; } 
        software[i] = ' ';
        }

        if(strcmp(result,"rev")==0){ 
        result = strtok(NULL,delims);  
        software = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(software, result);
        for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; } 
        software[i] = ' ';
        }

            if(strcmp(result,"DUT_board_id")==0){ 
        result = strtok(NULL,delims);  
        dutboardid = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(dutboardid, result);
        for(i=0;i<strlen(dutboardid)+1;i++){ if(dutboardid[i] == '\n') break; } 
        dutboardid[i] = ' ';
        }                   

            if(strcmp(result,"box")==0){ 
        result = strtok(NULL,delims);  
        corlbox = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(corlbox, result);
        for(i=0;i<strlen(corlbox)+1;i++){ if(corlbox[i] == '\n') break; } 
        corlbox[i] = ' ';
        }

        if(strcmp(result,"Tester_id")==0){ 
        result = strtok(NULL,delims);  
        testerid = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(testerid, result);
        for(i=0;i<strlen(testerid)+1;i++){ if(testerid[i] == '\n') break; } 
        testerid[i] = ' ';
        }

        if(strcmp(result,"@")==0){ 
        result = strtok(NULL,delims);  
        temp = (char*)malloc((strlen(result)+1)*sizeof(char));
        strcpy(temp, result);
        for(i=0;i<strlen(temp)+1;i++){ if(temp[i] == '\n') break; } 
        temp[i] = ' ';
        }


        result = strtok(NULL,delims);
    }

}

if (strcmp(customer, "0")==0){
customer = "N/A";
}
if (strcmp(device, "0")==0){
device = "N/A";
}
if (strcmp(testprog, "0")==0){
testprog = "N/A";
}
if (strcmp(software,"0")==0){
software = "N/A";
}
if (strcmp(dutboardid, "0")==0){
dutboardid = "N/A";
}
if (strcmp(corlbox, "0")==0){
corlbox = "N/A";
}
if (strcmp(testerid, "0")==0){
testerid = "N/A";
}
if (strcmp(temp, "0")==0){
temp = "N/A";
}

fprintf (ft, "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", in_file->d_name, customer, device, testprog, software, dutboardid, corlbox, temp, testerid);


printf(in_file->d_name);
printf("\n");
fclose (fs) ;
c++;

}
}
printf("Total Workorders Found: %d (Info saved to workorderlist.csv)\n", c);
    fclose ( ft ) ;
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-17T13:25:22+00:00Added an answer on June 17, 2026 at 1:25 pm

    If we take this example:

    customer = (char*)malloc((strlen(result)+1)*sizeof(char));
    strcpy(customer, result);
    for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
    customer[i] = ' ';
    

    It copies result, which takes up strlen(result)+1 bytes. You then copy in result into customer. After that, you search for a ‘\n’ in the string. What happens if there isn’t a ‘\n’ in the string? What value is i?

    I’m pretty sure the problem is that at some point or another, this style of code, is writing outside the allocated memory – since I don’t know exactly what your input is, I can’t say which of the several copies of this code is going wrong, but I’d bet that is what happens. That in turn means some data in the heap-structure itself is being corrupted, so at some point when malloc is called, it uses this bad data in the code.

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

Sidebar

Related Questions

I have code that reloads images via HTTP from the main thread and displays
I have some code that opens up a directory, some subdirectories and then creates
Here's our situation: We have some native code that scans user-defined directories at certain
I have an android application that scans the sd-card and plays file that are
I have some code that will stream video from a camera at 720p and
I have code that scans and returns NSString like this: NSString *GetText = [[NSString
I have an admin process that scans some directories and files to see if
Ok, i have some code that will scan my computer and find .txt files
I have code that generates a List<string[]> variable but can't quite figure out how
I have code that looks more or less like the code below but it

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.