Matt K
Matt K

Reputation: 147

retrieving a string with spaces from a file in C

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.

Upvotes: 1

Views: 2302

Answers (7)

pmg
pmg

Reputation: 108986

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

Upvotes: 0

AntonioMO
AntonioMO

Reputation: 896

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!

Upvotes: 0

Viliam
Viliam

Reputation: 4414

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.

Upvotes: 0

Sylvain Rodrigue
Sylvain Rodrigue

Reputation: 4994

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.

Upvotes: 0

Kelly S. French
Kelly S. French

Reputation: 12344

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.

Upvotes: 0

Rob Wells
Rob Wells

Reputation: 37159

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?

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994529

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().

Upvotes: 1

Related Questions