Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,311
    Rep Power
    2223
    Larry Wall, designer of Perl, is often quoted as saying, "There's more than one way." That is true of programming. There is more than one way to accomplish the same job, some more common, elegant, or efficient than others.

    The basic sub-task we're looking at is advancing the file pointer to the start of the next line. Here are two possible approaches:

    1. Since you are using fgetc to read the file one character at a time, I made one suggestion based on that approach. In that approach, you would continue to read characters from the file until you are about to read the next line. You detect that by having read the newline character(s), '\x0d' and '\x0a' -- in case you're not familiar with the hexadecimal notation, those are the characters whose ASCII values are 13 and 10, respectively, which are carriage return and line feed, respectively.

    Now, every single time fgetc reads a character from the file, it advances the file pointer by one character. However, fgetc will not have gone straight to the next line yet, because it still needs to go through whatever other characters are in that line, plus the newline characters. I don't know what that format is, meaning I don't know whether the ID is the only text on that line or if there's more text following it. Either way, fgetc will still need to detect and get past the newline characters.

    This is a lower-level approach, where you are working with each individual character.

    2. The second approach would be to not use fgetc, but rather to use fgets, which reads in an entire line from the file. It loads that line into a string, a char array, with the newline characters still attached. Then you can work with that string much as you already are doing; you just don't need to build the string yourself. This is a higher-level approach than the first one.

    When you call fgets, it reads in an entire line, which means that it positions the file pointer to the start of the next line. Thus, it does take you straight to the next line, whereas with fgetc you would need to do that explicitly. You can maintain a line counter if you would like, but that will not affect the operation of fgets.

    If the newline characters or extra text following the ID is a problem, there is a version of strcmp that will only compare a limited number of characters: strncmp.

    Be sure to read the documentation for fgets and for strncmp. Pay attention to the return value for fgets and to the behavior of strncmp.

    So, decide which approach you want to take and let us know.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0
    i think i will go with the second aproach, the sring one, as i have a slightly better understanding of strings. is the code below in any way right? i have declared temp as a char array [30] and readins a string into it from fpcust, then proceed to a strcmp with the user ID. My main question is how does fgets go to a new line after this code is executed? do i need to enter this into a loop or will it do it by itself?
    Code:
     
    while (!feof ( fpcust ) )
            {
                for (i = 0; i < len_string; i++)
                {
                    temp = fgets ( fpcust );
                }
            }
    
                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);
                }
  4. #18
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,311
    Rep Power
    2223
    Why haven't you read the documentation for fgets yet?
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0
    i just wanted to post that before i read it was all
  8. #20
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,311
    Rep Power
    2223
    Read what fgets does. Then you would see that that for-loop makes absolutely no sense at all.

    The loop should basically be: read a line, process it. Then the next time through the loop you will read the next line and process it. And again and again until you have read all the lines or found what you were looking for.

    Read what fgets does. And how to use it.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0
    have it all sorted and working now, thanks for all the help
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo