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 :
And my function :
Where`s the country is a parameter that is given to the function and is entered by the user .
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
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 INVALID; //If it will reach here - country is invalid = not in database .
Is there a fully convenient function that removes the need for to re-format the file ?