I have putten together this code for searching a text file for a users string (User's ID), how every i am aving some error that i dont knw how to fix and was hoping someone could help me. Thanks in advance.

#include<stdio.h>
#include<string.h>
#inlcude<stdlib.h>


int main()
{
    FILE *fpcust;
    int i, len_string;
    int searchID [30];
    int temp[30];

    printf("Please enter the user ID.");
    scanf("%d", &searchID);

    fpcust = fopen ("c:\\customers.txt", "r");
    rewind(fpcust);

    if ( fpcust == NULL)
    {
        printf("File cound not be opened.");
        exit(0);
    }

    len_string = strlen(searchID);

    while (!feof ( fpcust ) )
    {
        for (i = 0; i < len_string; i++)
        {
            temp[i] = fgetc ( fpcust );
            temp[i] = '\0';
        }

        //stricmp used for comparing both strings
        if ( stricmp ( searchID, temp ) == 0)
        {
            printf("The ID was found");
            fflush ( fpcust );
            fclose( fpcust);
            getchar();
            exit(1);
        }

        else
        {
            printf("No matches found.");
            getchar();
            exit (1);
        }

        fseek ( fpcust, -(len_string - 1), 1);
    }
    fclose ( fpcust );
}

What is the error?

Line `14 is incorrect -- remove the & pointer operator because character arrays are always passed as pointers.

You can delete line 17 because when the file is opened the file pointer is already at the beginning of the file, unless you use one of the open flags that sets it at the end of the file

line 32: why are you deleting the same character that was just read on line 31??? Move line 32 down outside the loop.

Even after you correct all the above it probably won't work in some cases. What happens if the string it is looking for is "John" but the string in the file is "Johnson"? That case will produce a false positive. A safer solution is to read an entire word from the file then compare the two. Instead of reading the file one character at a time all you have to do is call fscanf(fpcust,"%s", temp); which reads the whole word. Then delete lines 29-33.

delete line 39, no need to flush the stream at that point.

delete line 52, no reason to change the file pointer. The file pointer is already at the next position in the file. And it won't ever get executed anyway because the program will exit immediately after the first comparision due to the previous two exit() function calls.

line 54 will also never be executed for the same reason that line 52 won't.

Edited 3 Years Ago by Ancient Dragon

Type mismatch in parameter '__s1' (wanted 'const char *', got 'int *') in function search()
Cannot convert 'int *' to 'const char *' in function search()
Type mismatch in parameter '__s2' (wanted 'const char *', got 'int *') in function search()

these are the errors i am getting in my main progra where i have the above code as a function. i will try use what you said to fix these errors. thanks so much for the helpfull reply!

no sorry i didnt post the whole code i just posted the function but turned it into the main. this is the code here, but i have left out the other three functions as they are working ok.

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


//delcaring the function prototypes
void search (void);
void customer (void);
void sale (void);
void binary (void);


//declaring structure for new customer entry
struct customer
{
    int ID [20];
    int age;
    char gender;
    char name [40];
    char surname [40];
    char address [100];
};//end to customer sturucture


//declaring structure for new sale entry
struct sales
{
    int ID;
    int quantity;
    double price;
    char description [100];
};//end to customer sturucture


main()
{

    int choice = 0;
    printf("\n \n--------------- Shop Data Base --------------- \n \n");


    //printing out options
    printf( "Please enter one of the following: \n \n" );
    printf( "1. Search \n" );
    printf( "2. New Customer \n" );
    printf( "3. New Sale \n" );
    printf( "4. Convert To Binary \n \n" );

    printf("Your choice: ");


    //reading in the input
    scanf( "%d", &choice );


    if ( choice == 1 )
    {
        printf("You have selected the search function. Please follow the on screen instructions. \n");
        search();
        flushall();
    }
        if ( choice == 2 )
        {
            printf("You have selected the new customer function. Please follow the on screen instructions. \n");
            customer();
            flushall();
        }
            else if ( choice == 3)
            {
                printf("You have selected the new sale function. Please follow the on screen instructions. \n");
                sale();
                flushall();
            }
                else  
                {

                    printf("You have selected theconvert files to binary function. Please follow the on screen instructions. \n");
                    binary();
                    flushall(); 
                }


    getchar();
    flushall();


    return 0;

}//end to main


//function search
void search (void)
{
    FILE *fpcust;
    int i, len_string, lenght;
    int searchID [30];
    int temp[30];

    printf("Please enter the user ID.");
    scanf("%d", &searchID);

    fpcust = fopen ("c:\\customers.txt", "r");
    rewind(fpcust);

    if ( fpcust == NULL)
    {
        printf("File cound not be opened.");
        exit(0);
    }


    len_string = strlen(searchID);

    while (!feof ( fpcust ) )
    {
        for (i = 0; i < len_string; i++)
        {
            temp[i] = fgetc ( fpcust );
            temp[i] = '\0';
        }

        //strcmp used for comparing both strings
        if ( strcmp ( searchID, temp ) == 0)
        {
            printf("The ID was found");
            fflush ( fpcust );
            fclose( fpcust);
            getchar();
            exit(1);
        }

        else
        {
            printf("No matches found.");
            getchar();
            exit (1);
        }

        fseek ( fpcust, -(len_string - 1), 1);
    }
    fclose ( fpcust );
}

Type mismatch in parameter '__s1' (wanted 'const char *', got 'int *') in function search()

That makes no sense at all -- search() doesn't take any parameters so how could that error occur??? Don't waste our time by posting nonsense code.

Edited 3 Years Ago by Ancient Dragon

relax i didnt mean to waste your time. you helped me with the problem i was having, then you asked if i posted the correct code and i told you that i just changed the function to the mai and then posted the real thing just incase you wanted to see it!

Yes, but the code you posted doesn't match the errors you are getting. How do you expect anyone to help you if you don't post the correct code??? Or maybe you are not compiling the program you think you are compiling, you could be compiling an old version of the program, which happens occasionally (been there and done that.)

Edited 3 Years Ago by Ancient Dragon

nope i compiling the right code and i fixed the problem with the help you gave me yester which i am very thankful for.

This question has already been answered. Start a new discussion instead.