1 year ago

#351753

test-img

JakeFist26

How do I read a single letter from a file when the letter has a space on either side? (C)

I'm currently working on an assignment in my Intro to C class. The assignment has us using fgets to read a single line from a file, then strcpy and strtok to take individual elements from that line and store them into different arrays. The file is a list of comic book character info, so we would store the character's first name into an array, last name in a different array, age into a different array, etc. Here is an example of one line from the file:

Romanoff , Natasha ; F Black Widow 1964 Marvel 52 Avengers 1975 green Human Auburn 170 59

I've had success storing the last and first names using strtok to stop at the punctuation separating the names. I'm running into a problem with the next bit, however, which is storing the character's sex (in this case, "F") into an array for gender. Since there is no punctuation or numbers between it and the next bit of info, I'm not sure how to proceed.

Initially, I tried reading the character from the file in the same way that I did the other items, using a space " " as the delimiter in strtok:

strcpy(gender[count], strtok(NULL, " "));

When I do this, I receive the following error:

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]

I'm thinking that strtok is picking up on the space immediately following the ";" and stopping there before even getting to "F" and copying that to gender[count]. But I don't know how to skip over that space so that the "F" is successfully copied.

arrays

c

file

strtok

strcpy

0 Answers

Your Answer

Accepted video resources