0

Whenever i use fscanf to read in file contents which in this case is a country name followed by a space, country capital followed by a space, and then the population, it is read in successfully until the name of country differs such as "united arab emirates". I'm using all the advice ive been able to find and still cannot seem to have it parse until the new line operator. It instead stops scanning after united and leaves emirates for fscand to read into my country capital value which then throws everything off. Any suggestions will help as i am a fairly new c programmer.

terminal

 egypt                  cairo 81015887.000000
 madagascar             antanonariva 21926221.000000
 zimbabwe               harare 12521000.000000
 united                 arab 0.000000   
 mirates                abu 0.000000   
 habi                    8264070 0.000000   
 nited                     states 0.000000   
 ashington,                         d. 0.000000   
 .                  312642000 0.000000   
 osnia                herzegovina 0.000000

main.cpp

#include <stdio.h>    //for testing within main
#include <string.h>  //for testing within main
#include <stdlib.h>
#include "headers.h"

int main()
{   
    int size, i, g;  //size of countries that will be read in
char usrC;   //user choice
FILE * fileP;
char UI[25];

Country * c[10];

ask(UI);
size = oSesame(UI, fileP, c);

if(size==0)     //if file returns as empty
{
    return 0;
}

for(g=0;g<size;g++)
{
    printf("%s %26s %-11lf\n ", c[g]->cntName, c[g]->capName, c[g]->population);

}


return 0;
}

header.h

#define COUNTRY_MAX 10
#define MAX_COUNTRY_LENGTH 25
#define MAX_CAPITAL_LENGTH 25

typedef struct country
{
    char cntName[MAX_COUNTRY_LENGTH];
    char capName[MAX_CAPITAL_LENGTH];
    double population;

}Country;

int ask(char * usrTxt);
int oSesame(char usrTxt[], FILE * pFile, Country * p[]);

promp.c

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "headers.h"


int ask(char * usrTxt)
{
    int length;
    printf("Enter text file to read from :  ");
    scanf("%s", usrTxt);
    length = strlen(usrTxt);
}



int oSesame(char usrTxt[], FILE * pFile, Country * p[10])
{

int count = 0;
int savedOffset;
pFile = fopen(usrTxt, "rw");

if(pFile != NULL)
{
    savedOffset = ftell(pFile);  //save how far from beginning of file we start
    fseek(pFile, 0, SEEK_END);   //seek to end of file

    if(ftell(pFile) == 0)       //if end of file is same location as beginning then file is empty
    {
        printf("File %s is empty", usrTxt);   
        return 0;           //end program

    }
    else{   
        fseek(pFile, savedOffset, SEEK_SET);        //set location indicator to beginning to begin parsing

        while(count<COUNTRY_MAX && !feof(pFile))
        {
            p[count] = (Country *) malloc(sizeof(Country));   //allocate memory for our structures since they havnt actually been declared yet just pointers to potential ones
            fscanf(pFile, "%s[^\n]", p[count]->cntName); //read in as string until newline character found in regex
            fscanf(pFile, "%s[^\n]", p[count]->capName);                
            fscanf(pFile, "%lf", &p[count]->population);
            getc(pFile);  //get new line character

            count++;
        }
        fclose(pFile);
    }
}
else
{
    printf("Error opening %s , check your spelling and try again.\n", usrTxt);
}
return count;
8
  • 2
    while(count<COUNTRY_MAX && !feof(pFile)) please read the man page for feof for correct usage. Instead, check the return value from fscanf and getc. Sep 15, 2015 at 20:43
  • If the format of your file involves variable-length strings delimited by space characters, then do you not recognize the inherent problem with those string containing internal space characters? Sep 15, 2015 at 21:33
  • @JohnBollinger elaborate please Sep 15, 2015 at 21:35
  • @ColinRickels, if a space character may be either data or a delimiter, then how is the program supposed to tell which any particular one is? Sep 15, 2015 at 21:36
  • 1
    Perhaps it would be helpful if you presented the input file, or at least a representative portion of it. Sep 15, 2015 at 21:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.