What will be the better way of reading a file ?

This is a discussion on What will be the better way of reading a file ? within the C Programming forums, part of the General Programming Boards category; Greetings everyone , A quick question here about file management , I`m having a problem which I could not overcome ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    11

    [SOLVED] What will be the better way of reading a file ?

    Greetings everyone ,

    A quick question here about file management , I`m having a problem which I could not overcome and I`d like to hear some suggestions .

    The case is this - I`m coding a "social network" kind of project , and in the registration part I would like to verify the entered country ,
    my thought was to include a file with most/all of the countries and loop there for to check for a valid input .

    The problem is that I could not find a function which really suits for reading from the file , if I use fscanf than every space is considered as a delimiter
    , hence dividing the strings of countries with spaces in it (as United Stated) ,
    and if I use fgets then the input is somehow weird -
    The first character from each country is not inputted , and I think that the fixed size one specifies in the function is the reason for those errors .

    My file (country.txt) has one country each line :
    Afghanistan
    Albania
    Algeria
    Andorra
    ...

    And my function :
    Code:
        char tempCountry[MAX_COUNTRY_LENGTH];       //Temporary variable . (MAX_COUNTRY_LENGTH=31)
        FILE *f_country=fopen("country.txt","r");   //Country file .
        while(fgetc(f_country)!=EOF)                //Go till EOF
        {
            strcpy(tempCountry,"");
            fgets(tempCountry,sizeof(tempCountry),f_country);       //Get the current line from the file .
            if(strcmp(country,tempCountry)==NULL)      //If there is a match - return VALID (country is valid).
                return VALID;
        }//while
        return INVALID;                                //If it will reach here - country is invalid = not in database .
    Where`s the country is a parameter that is given to the function and is entered by the user .

    Is there a fully convenient function that removes the need for to re-format the file ?

    Thanks ,

    Danny .
    Last edited by Dann Shem; 06-26-2011 at 02:58 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets includes the \n at the end of the line. So just snip it off and you should be fine. You also may want to lower case everything, so that 'Algeria' and 'AlgeriA' are the same thing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    1
    The first character of each country is missing because of the fgetc() call:
    each time it will eat up one character from the input stream.
    Try something like that:

    Code:
    while(fgets(tempCountry,sizeof(tempCountry),f_country))                //Go till EOF
        {
        int len = strlen(tempCountry);
        if (len && tempCountry[len - 1] == '\n')
            tempCountry[len - 1] = 0; // remove trailing \n
    
        if(!strcmp(country,tempCountry))      //If there is a match - return VALID (country is valid).
    Last edited by grumly; 06-26-2011 at 02:49 AM.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    Quote Originally Posted by grumly View Post
    The first character of each country is missing because of the fgetc() call:
    each time it will eat up one character from the input stream.
    Try something like that:

    while(fgets(tempCountry,sizeof(tempCountry),f_coun try)) //Go till EOF
    {
    int len = strlen(tempCountry);
    if (len && tempCountry[len - 1] == '\n')
    tempCountry[len - 1] = 0; // remove trailing \n

    if(!strcmp(country,tempCountry)) //If there is a match - return VALID (country is valid).
    I have to take note here... if you are going to use strlen to cut off the \n, why not just use strncmp
    and don't bother cutting off the \n at all:
    Code:
    if (strncmp(country,tempCountry,strlen(tempCountry)-1) == 0)
        ...
    better still I would think they have the length of "country" previously calculated then removing the need for the
    strlen() call at all and making it much better:
    Code:
    if (strncmp(country,tempCountry,lenofcountry) == 0)
        ...
    either case removing the need to cut off the \n
    Last edited by nonpuz; 06-26-2011 at 02:55 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    Thank you guys , my problem is now solved .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  2. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  3. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  4. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 07:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21