Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0

    Searching Text File


    I am having some serious trouble with the following code. what its mean to be doing is searching the user Id in the text file and returning if it was found or not. I am new to c and dont know how to get rid of the errors i am getting so any help will be very helpful! Please help me!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //delcaring the function prototypes
    void search (void);
    
    main()
    {
        
        search();
        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");
                fclose( fpcust);
                getchar();
                exit(1);
            }
            
            else
            {
                printf("No matches found.");
                getchar();
                exit (1);
            }
            
            //~ fseek ( fpcust, -(len_string - 1), 1);
        }
        fclose ( fpcust );
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I am new to c and dont know how to get rid of the errors i am getting
    What are your errors? Please post the exact error messages exactly as they appear in your development environment.

    Also appears to be posted here.

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    yea there is 2 of us working o the same project that must be my partner, these are the errors
    Error E2034 search.c 46: Cannot convert 'int *' to 'const char *' in function search()
    Error E2342 search.c 46: Type mismatch in parameter '__s' (wanted 'const char *', got 'int *') in function search()
    Error E2034 search.c 57: Cannot convert 'int *' to 'const char *' in function search()
    Error E2342 search.c 57: Type mismatch in parameter '__s1' (wanted 'const char *', got 'int *') in function search()
    Error E2034 search.c 57: Cannot convert 'int *' to 'const char *' in function search()
    Error E2342 search.c 57: Type mismatch in parameter '__s2' (wanted 'const char *', got 'int *') in function search()
    Warning W8080 search.c 75: 'lenght' is declared but never used in function search()
    *** 6 errors in Compile ***
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Error E2034 search.c 46: Cannot convert 'int *' to 'const char *' in function search()
    Error E2342 search.c 46: Type mismatch in parameter '__s' (wanted 'const char *', got 'int *') in function search()
    So what exactly don't you understand about these two error messages?

    Just to give a hint this is the line in question (line 46):

    Code:
        len_string = strlen(searchID);
    Did you find and study the documentation for this function strlen()? I


    Jim
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    main returns int. Please declare it explicitly as returning int.

    scanf("%d", searchID);
    You declared searchID as an int array, which is an int* . You're telling scanf to read in one single decimal value, so you need to tell it which element of searchID you want to store it in; eg, &searchID[0].

    len_string = strlen(searchID);
    strlen is meant to work with C-style strings, which are char arrays. You are passing it an int array, which is wrong. The same applies to your passing the int array, searchID, to strcmp.

    If you want to work with strings, then work with strings, which are char arrays. If you meant searchID to be a string, then you should declare it as a char array and you would read it in with scanf using the "%s" descriptor flag. But since I don't know what searchID is supposed to be, I'm just making an assumption here.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    thats for the help guys, i changed them to strings as you suggested and that got rid of the errors. i understand now as i was declaring the variables wrong. Lessoned learned. Thanks for the help!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    The lesson that needed to be learned was understanding what the error messages were telling you.

    Part of the learning experience is to learn through experience what kinds of mistakes on your part generates what kinds of error and warning messages.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Another part of the learning experience that needs to be learned is how to locate and read the documentation for the standard functions you're trying to use.

    Jim
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    looking back on the error i can see now that it was telling my i cant pass the int and needed to pass the cahr string so i understand that now and can see it next time i use code like this
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    do you know of a line of code that will keep the application open until i hit i key? at the moment im using getchar(); and fklushall(); but they are a bit iffy sometimes.
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    How's about running the console application from the console (ie, command line) as it was intended to be run? But if you insist on launching it from a GUI, then you'll have to resort to a kludge.

    getchar() will just read the next character input, but if scanf has left a newline in the input buffer as it usually does, then getchar() will just read in that newline character and blow on through. Piling on getchar() calls might work better, but you still can't predict how many characters were left in the buffer.

    Do not use fflush(stdin)! fflush's behavior is only defined for output buffers, not for input buffers. Avoid depending on undefined behavior; one forum member describes it as "dancing barefoot on broken glass."

    This should force the entry of a printable character:
    scanf(" %c", ch);
    Please note the space before the percent sign, which tells scanf to ignore all white space (newline, tabs, space) that precede the character you're interested in. The down-side of this is that it should ignore your hitting the Enter key or space bar; you would have to enter an actual printable character plus the Enter key. But at least it's much more reliable.

    Or you could design for the future in anticipation of console apps with a main menu. Such a menu has an explicit exit command. Or you could demand a specific quit command, which could be entering a specific character, which you would prompt for.

    Just some ideas. What do I use? None of the above, because I run mine from the command line as they were intended to be run.

    PS
    A couple more thoughts.

    Another kludge is to open a sub-shell to run another app that will exit as soon as you hit a key. You open this sub-shell with the system() function and in Windows you would call the PAUSE shell command; thus:
    system("PAUSE");
    All PAUSE does is display a "pause" message and then as soon as you press a key it terminates. If you're using Linux, I don't know what the equivalent shell command would be.

    Some IDEs either automatically perform a pause after the program terminates or can be set to do that. Tell us what IDE (AKA "compiler") you're using and someone may be able to tell you whether it does this or not. I've heard that there might be a console setting in Windows that would also do the same thing.
    Last edited by dwise1_aol; April 24th, 2013 at 11:52 AM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    wow all this help is overwhelming thanks you! i have seen system("PAUSE") before but never understood it so i didnt use it. i am using a compiler called borland. my lecture told us to use this one starting off as it very simple and easy to navigate
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    ok so i have been messing around with the code and it sees not too be working again. there is no errors however. the code is searching for the file but it is only searching the first line, i have been trying to use fseek to make it move to the next line but i amnt too sure if i can even do that or do i have to use something else? the code is as follows
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //delcaring the function prototypes
    void search (void);
    
    main()
    {
        
        search();
        flushall();
        
        getchar();
        flushall();
        
    
        return 0;
        
    }//end to main
      
    
    //function search
    void search (void)
    {
        FILE *fpcust;
        int i, len_string;
        char searchID [30];
        char temp[30];
        
        printf("What do you want to search for? \n");
        scanf(" %s", searchID);
        
        fpcust = fopen ("c:\\presdatatest.txt", "r");
        rewind(fpcust);
        
        if ( fpcust == NULL)
        {
            printf("File cound not be opened.");
            exit(0);
        }
        
        else
        {
            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 \n");
                    fclose( fpcust);
                    system ("PAUSE");
                    exit(1);
                }
                
                else
                {
                    printf("No matches found. \n");
                    system ("PAUSE");
                    exit (1);
                }
                
                fseek ( fpcust, len_string, SEEK_SET);
            }
            fclose ( fpcust );
        }    
    }
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    SEEK_SET means that you move to the given offset from the beginning of the file. So you keep returning to the same location over and over and over again.

    Did you read the documentation on fseek? I know that Turbo C and Borland C++ had good help files, though I understand that the free download versions may not have them. You can find the man pages (the UNIX documentation for standard C library functions) on-line through Google; eg, man page fseek.

    Also, do you understand file I/O? What you're playing with when you use fseek and ftell is what's called the file pointer. The file pointer marks the current location in the file, which is to say the location that will be read from next. As you read through a file, the file pointer is advanced automatically. That means that if you read everything from the file in sequence, then you never need to use fseek. I should also add that fseek is more commonly used with binary files than with text files; in 23 years I've not seen it used with text files.

    What's the file format? Is it a text file with multiple lines, each line (except possibly the last one) ending with a newline sequence? And is the ID that you're looking for always at the beginning of the line that it's in?

    If that is the case, then what you could do to advance to the next line would be to call fgetc repeatedly until you get past the newline (CR-LF, ie "carriage return-line feed", which are the characters '\x0d' and '\x0a', respectively, though you may be able to work with them as "\r\n" (double-check the order there) ). After the newline character(s) is the beginning of the next line.

    Though a simpler way would be to use fgets, which gets an entire line from the file, then you can search that string for the substring that would be the ID.

    But the important lesson you should walk away with is what Jim had pointed out: learn to read the documentation for the standard library functions.
    Last edited by dwise1_aol; April 24th, 2013 at 02:45 PM.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    14
    Rep Power
    0
    yea i am learning alot today just from this thread it si very help full. so you say to use fgetc instead, so will i just enter gfetc into a loop then read what i am getting from that into an array and then compare that array with my ID? will fgetc go straight to the second line when it is going through the loop again or will i need to increment a 'line counter' say every time i enter the loop?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo