views:

249

answers:

8

We were given an assignment that involved taking information from a file and storing the data in an array. The data in the file is sorted as follows

New York            40 43 N    74 01 W

the first 20 characters are the name of the city followed by the latitude and longitude. latitude and longitude should be easy with a few

fscanf(infile, "%d(or %c depending on which one i'm getting)", pointer)

operations so they won't be a problem.

My problem is that i do not know how to collect the string for the name of the city because some of the city names have spaces. I read something about using delimiters but from what i read, it seems like that is used more for reading an entire line. Is there any way to read the city name from a file and store the entire name with spaces in a character array? Thanks.

+1  A: 

Here's a hint: With spaces as your only delimiter, how would you tell fscanf() where the city name starts and the latitude starts? You're getting close with your "it seems like that is used more for reading an entire line". Explore that, perhaps with fgets().

Greg Hewgill
so with fgets are the arguments (inFile, number of characters to scan, pointer)? i remember hearing about that commad but i forget how it works. because if that is how it works, could i just putfgets(inFile, 20, cityName); ?
Matt K
DO NOT use fgets() it can never be used safely.
Heath Hunnicutt
@Heath: do you mean `gets()`?
pmg
Why not? gets() can never be used safely, but with fgets() you specify the maximum size of your buffer, so properly used you won't have buffer overruns, so fgets() is perfectly usable.
machielo
A: 

G'day,

As you're scanning up until the first number, the latitude, for your city name maybe use a scan for non-numbers for the first item?

Rob Wells
how do I scan for non numbers?
Matt K
Use `isdigit`: http://www.elook.org/programming/c/isdigit.html
Jacob
Thank you. I'll give it a try.
Matt K
A: 

If you have spaces in your city name, you either need to use delimiters or define the city name field to be fixed length. Otherwise trying to parse a three-word city name, e.g. "Salt Lake City", will kill the next field.

Kelly French
A: 

Just a hint : read the entire line in memory and then take the first 20 chars for the city name, the next, say 10 chars for latitude and so on.

Sylvain
But i thought that an array could only contain 1 type. Wouldn't that be combining ints and chars in whatever i store the read string in?
Matt K
He means read the entire line into memory as a string (that is, read into an array of char). Once you've chopped that into bits, functions such as `strtol` can convert strings representing numbers, into numbers.
Steve Jessop
A: 

You can specify size for %c which will collect a block of characters of the specified size. In your case, if the city name is 20 characters long put %20c inside the line format of scanf.

Then you have to put terminator at the end and trim the string.

Viliam
That sounds pretty easy. Thanks.
Matt K
+1  A: 

Just a hint : read the entire line in memory and then take the first 20 chars for the city name, the next, say 10 chars for latitude and so on.

Sylvain
A: 

From "man fgets":

char *fgets(char *s, int size, FILE *stream);

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

This means that you need a char array of 21 chars to store a 20 char string (the 21st char will be the '\0' delimiter at the end, and fgets will put it automagically).

Good luck!

machielo
A: 

scanf() can take a limited amount of characters with the "%c" specifier.

ATTENTION It will not add a terminating null byte.

char cityname[21];
scanf("%20c%d%d %c%d%d %c", cityname,
      &lat1, &lat2, &lat_letter,
      &lon1, &lon2, &lon_letter);
cityname[20] = 0;

But you're better off using fgets() and parsing the string "manually". Otherwise you're going to have END-OF-LINE issues

pmg
Thanks for the help., that should work fine.
Matt K