1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    Beginner problem with input from textfile

    Hello everyone.

    As the title suggests, I am a beginner with C development.

    I was given an assignment (don't worry, I'm not looking for you to do my homework :P) to do a couple days ago where I need to read input from a file called Data.txt which has the following info:

    FirstName LastName Nationality Result
    Aleksandr Menkov Russia 8.09
    Aleksandr Petrov Russia 7.89
    Alyn Camara Germany 7.72
    Arsen Sargsyan Armenia 7.62
    Boleslav Skhirtladze Georgia 7.26
    Christian Reif Germany 7.92
    Christopher Tomlinson Great Britain 8.06
    Damar Forbes Jamaica 7.79
    Eusebio C?ceres Spain 7.92
    George Kitchens United States 6.84
    Godfrey Khotso Mokoena South Africa 8.02
    Greg Rutherford Great Britain 8.08
    Henry Frayne Australia 7.95
    Ignisious Gaisah Ghana 7.79
    Li Jinzhe China 7.77
    Lin Ching-Hsuan Chinese Taipei 7.38
    Lo?is Ts?toumas Greece 7.53
    Luis Rivera Mexico 7.42
    Marcos Chuva Portugal 7.55
    Marquise Goodwin United States 8.11
    Mauro Vinicius da Silva Brazil 8.11
    Michel Torn?us Sweden 8.03
    Mitchell Watt Australia 7.99
    Mohamed Fathalla Difallah Egypt 7.08
    Mohammad Arzandeh Iran 7.84
    Ndiss Kaba Badji Senegal 7.66
    Povilas Mykolaitis Lithuania 7.61
    Raymond Higgs Bahamas 7.76
    Roman Novotn? Czech Republic 6.96
    Salim Sdiri France 7.71
    Sebastian Bayer Germany 7.92
    Sergey Morgunov Russia 7.87
    Stanley Gbagbeke Nigeria 7.59
    ?tep?n Wagner Czech Republic 7.5
    Supanara Sukhasvasti Thailand 7.38
    Tyrone Smith Bermuda 7.97
    Vardan Pahlevanyan Armenia 6.55
    Viktor Kuznyetsov Ukraine 7.5
    Will Claye United States 7.99
    Zhang Xiaoyi China 7.25

    My problem right now is that when read through the text and assign the text to variables of firstName, lastName, nationality, and distance to the arrays of Athlete it doesn't collect the whole thing for the nationality variable. Eg. If a nationality is Great Britain, it just gets the "Great" part and throws everything off afterwards. Is there any fix to this I'm not seeing? Below is my code to load in the data to the array:
    void loadData(char filename[], Athlete a[])
    char tmpFname[20];
    char tmpLname[20];
    char tmpNat[15];
    char tmpDist[15];
    int counter;  	// declare a file pointer 
    FILE *input= fopen(filename, "r+");  	
    //check if the file opened succesfully    
    if (input==NULL) 		
    perror ("Error opening file");    
    		printf("All data successfully loaded \n"); 		
    // continue in reading the file til the EOF 		
    strcpy(tmpFname," "); 			
    fscanf(input, "%s %s %s %s", tmpFname,tmpLname,tmpNat,tmpDist); 
    Any help would be appreciated. I'm gonna try and work on getting this to work and check back here in a bit. I expect a lot of :chomp:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    A keyword here is "delimiter". When you parse a string, you do so with delimiters. For fscanf using %s, a space is the delimiter. For CSV files ("comma-separated values"), commas and quotation marks are used to properly delimit the values in the fields; in Excel, you can specify the delimiter for CSV files, including tabs.

    But your file format leaves you with spaces. So instead of a properly specified delimiter, you need to be smart about it.

    First thought: the problem is the name of the nation. You could maintain a list of all possible nations. If the first national string you encounter is one that belongs to a two-word name (eg, "Great" or "United"), then you would know to examine the second and even the third or fourth fields.

    Second (better) thought:
    Parse out all the space-delimited strings in the line. You know which field starts the name of the nation. You know that the last field, Dist, is not part of the nation's name, but that the field immediately before it is. So then, mark the first field that should be part of the nation's name, then mark the last field of the nation's name, and all fields therebetween, including the first and the last, belong to the nation's name.

    Now consider a technique. If you simply call fscanf, then if you ask for more strings than are present on that line, it will merely move on to the next line to fulfill your request for more strings. Instead, fgets one entire line, then use sscanf on it, asking for the maximum number of strings on that line. The return value of sscanf tells you how many conversions were successful, so it should tell you how many fields it read from that line. The rest should be obvious.

IMN logo majestic logo threadwatch logo seochat tools logo