当字母在两边都有一个空间时,如何读取文件中的单个字母? (C)

发布于 2025-01-18 03:07:36 字数 799 浏览 3 评论 0原文

我目前正在从事C级介绍中的作业。分配使我们使用fgets从文件中读取一行,然后strcpystrtok从该行中获取单个元素并存储它们变成不同的数组。该文件是漫画字符信息的列表,因此我们将角色的名字存储到数组中,姓氏在不同的数组中,年龄为其他数组等。这是文件中一行的示例:

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

i 'Ve成功地使用Strtok存储了最后一个名字和名字,以停止将名称分开的标点符号。但是,我遇到了下一个问题的问题,但是将角色的性别(在这种情况下为“ F”)存储在性别阵列中。由于它与下一个信息之间没有标点符号或数字,因此我不确定如何进行。

最初,我尝试以与其他项目相同的方式读取文件的字符,使用一个空间“” strtok中的定界符:

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

当我这样做时,我会收到以下错误:

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

我认为Strtok是紧随“”;并在到达“ f”之前就停止那里,然后将其复制到性别[count]。但是我不知道如何跳过该空间,以便成功复制“ F”。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文