简体   繁体   中英

Combining two programs with pipe won't work

I am supposed to make two programs:

  • First program takes a command line argument which is a number, and then it takes more inputs after execution, and if any those equal the command line number you entered, it comes back true, else it comes back false.
  • The second program generates a bunch of numbers, and if you want you can give it a seed.

Both of these programs work independently correctly, it stops working when I try to pipeline them ( ./generate 1000 50 | ./find 817 ).

Usage:

./find #

if number is found: output true, else false

./generate [seed]

Seeds are generated.

When i try and combine these two commands it doesn't work, I'll see that the generate command generated a number, and wrote ./find for that number, but it doesn't come out as true.

Source code for find.c

/**
 * Prompts user for as many as MAX values until EOF is reached, 
 * then proceeds to search that "haystack" of values for given needle.
 *
 * Usage: ./find needle
 *
 * where needle is the value to find in a haystack of values
 */

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

#include "helpers.h"

// maximum amount of hay
const int MAX = 65536;

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: ./find needle\n");
        return -1;
    }

    // remember needle
    int needle = atoi(argv[1]);

    // fill haystack
    int size;
    int haystack[MAX];
    for (size = 0; size < MAX; size++)
    {
        // wait for hay until EOF
        printf("\nhaystack[%i] = ", size);
        int straw = get_int();
        if (straw == INT_MAX)
        {
            break;
        }

        // add hay to stack
        haystack[size] = straw;
    }
    printf("\n");

    // sort the haystack
    sort(haystack, size);

    // try to find needle in haystack
    if (search(needle, haystack, size))
    {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    }
    else
    {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

more find.c source code

 /**
 * helpers.c
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm
    if(values[4] < 0) {
        return false; 
    }
    if(value < 4) {
        printf("Valid usage: ./search array value\n"); 
        return 52;
    }
    //

    for( long long i = 0 ; i < n ; i++ )
    {
        if (value == values[i])
        {
            return true;
        }

        printf("%i", values[i]);    
    }

    return false;
}

/**
 * Sorts array of n values.
 */


void sort(int values[], int n)
{

    int smallest = values[0];
    int smallestSpot = 0;
    for (long long i = 0; i < n ; i++)
    {

        for(long long j = i; j < n - i ; j++) //find the smallest int in array
        {

            if(values[j] < smallest)
            {
                smallestSpot = j;
                smallest = values[j];
            }

            values[smallestSpot] = values[i];
            values[i] = smallest;

        }

    }
    return;
}

./generate source code

/**
 * generate.c
 *
 * Generates pseudo random numbers in [0,MAX), one per line.
 *
 * Usage: generate n [s]
 *
 * where n is number of pseudo random numbers to print
 * and s is an optional seed
 */

#define _XOPEN_SOURCE

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// upper limit on range of integers that can be generated
#define LIMIT 65536

int main(int argc, string argv[])
{
    // Make sure user gave enough inputs
    if (argc != 2 && argc != 3)
    {
        printf("Usage: ./generate n [s]\n");
        return 1;
    }

    // convert the string that is inputted to an integer
    int n = atoi(argv[1]);

    // if user gives a seed, use that seed
    if (argc == 3)
    {
        srand48((long) atoi(argv[2]));
    }
    else 
    {
        srand48((long) time(NULL));
    }

    // create this amount of random inputs
    for (int i = 0; i < n; i++)
    {
        printf("%i\n", (int) (drand48() * LIMIT));
    }

    // success
    return 0;
}

排序覆盖了./generate中的前一半左右的条目。

Your search program is too complicated: it is useless to sort the input and it is actually incorrect to try and read all of the input for this purpose:

  • reading all lines into an array is a waste of space.
  • sorting is a waste of time, comparing each input as it is read is simpler and faster.
  • your sorting algorithm is bogus: you do not swap the smallest value into the next index, you just overwrite the value there, the misplaced value is lost.
  • you could break out of the loop as soon as the needle is found.

Here is a modified version:

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

#include "helpers.h"

// maximum amount of hay
const int MAX = 65536;

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

    // ensure proper usage
    if (argc != 2) {
        printf("Usage: ./find needle\n");
        return 2;
    }

    // convert needle
    int needle = atoi(argv[1]);
    int found = 0;

    // parse input
    for (int count = 0; count < MAX; count++) {
        // wait for hay until EOF
        int straw = get_int();
        if (straw == INT_MAX) {
            break;
        }
        if (straw == needle) {
            found = 1;
            break;
        }
    }

    // report status
    if (found) {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    } else {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM