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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:14:36+00:00 2026-06-17T10:14:36+00:00

I have a code that scans all the files in a directory for targeted

  • 0

I have a code that scans all the files in a directory for targeted words, and prints them out into a new file. The problem right now is after the while loop reads a file and stores a variable into the string (ex. customer), if the next file being read does not have the targeted word, it still displays the result stored in the string from the previous file. My goal is to make it display “N/A” if the current file does not have the target word.

I have tried a few ways to clear the string at the end or beginning of the while loop, but none of them work most of them just gives me a coredump error. Running out of ideas, any help would be much appreciated!

Code (shortened for easier reading):

int main(int argc, char** argv)
{   
    char           directory[100];
    char           buff[100];
    char           delims[] = " :=";
    char*          result = NULL;
    char*          customer;
    char*          device;
    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_Prog,Software,DUT_board_id,Corl box\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(100);
        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 */

            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, "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] = ' ';
                    }

                    result = strtok(NULL,delims);
                }   
            }

            if (customer == '\0')
            {
                customer = "N/A";
            }

            if (device == '\0')
            {
                device = "N/A";
            }

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

             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-17T10:14:37+00:00Added an answer on June 17, 2026 at 10:14 am

    First at all, customer/device are strings. You should not be doing == for it comparison. You can, for example, compare the first char of the string: device[0] == '\0';
    You should do string initialization before the loop starts.
    You can achieve this by using strcpy with a known value or any other string manipulation function. The value that you use to initialize the string before the loop is the one you gonna test with strcmp or similar later.
    Is like with ints or any other C data type, but you need manipulation functions instead.

    By the way, haven’t you posted your read file loop in a question here too?

    Hope this helps.

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

Sidebar

Related Questions

I have a code that lists all files in a dir. The code looks
I have code that generates a List<string[]> variable but can't quite figure out how
I have written a service that monitors a file drop location for files from
all. I have an app that scans a picture folder and displays the images
I have a parser function written in my code-behind file, that takes as input
I have a Hierarchical project that uses source code from a common system-directory, for
I'm currently using the following code to scan files that have been uploaded as
I have code that looks more or less like the code below but it
I have code that sends web requests through two parallel for each loops. Will
I have code that reloads images via HTTP from the main thread and displays

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.