retrieving a string with spaces from a file.

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)

operation. 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.

Comments

  • [color=Blue]Use fgets() instead of fscanf() to get the whole iine. Then simply scan the text until first digit is met.

    Also, try the %20s format for first parameter, because the requirement says something about first 20 characters being a name.[/color]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion