There is nothing wrong with reading from a file up-to the first space character with fgetc, however, there are several problems with:if (fgetc(grades) != ' '){ currChar = fgetc(grades); className[i] = currChar; i++;}当您调用fgetc(grades) != ' '时,将从grades中读取一个字符,然后 file-position-indicator 前进到下一个字符.因此,当您在if语句中调用fgetc时,您将读取一个字符,然后前进 file-position-indicator 以准备读取下一个字符.然后,如果在if语句(currChar = fgetc(grades);)的正文中再次调用fgetc,则将读取文件中的 second 字符.从您的问题看来,很明显,您的意图不是跳过一个字符,而是从下一个开始阅读.When you call fgetc(grades) != ' ', a character is read from grades and then the file-position-indicator is advanced to the next character. So when you call fgetc inside the if statement, you read a character and then the file-position-indicator is advanced in preparation for reading the next character. When you then call fgetc again in the body of the if statement (currChar = fgetc(grades);), you read the second character in the file. It seems rather obvious from your question, that your intent was not to skip-a-character and then begin reading with the next.当您使用任何面向字符输入功能(例如getchar,fgetc等)读取输入时,您要负责三件事:(1)确保您自己不要尝试向存储变量中写入超出其容纳范围的字符; (2)检查您希望停止阅读的任何前哨或终止字符;和(3)检查您尚未到达输入流的末尾.When you read input with any character-oriented-input function (e.g. getchar, fgetc, etc.) there are three primary things you are responsible for: (1) insuring you do not attempt to write more characters to your storage variable than it will hold; (2) check for any sentinel or terminating character you wish to stop the read with; and (3) checking that you have not reached the end of the input stream.将这些片段放在一起,您可以执行以下操作:Putting those pieces together you could do something like:#include <stdio.h>#define MAXCN 10int main (void){ FILE* grades = NULL; /* initialize variables */ char className[MAXCN] = {0}; int currChar = 0; /* currChar is an int */ int i = 0; /* open and validate file */ if ((grades = fopen("grades.txt", "r")) == NULL) { fprintf (stderr, "error: file open failed 'grades.txt'.\n"); return 1; } /* read from grades until 1st space */ while (i + 1 < MAXCN && ((currChar = fgetc (grades)) != ' ' && currChar != EOF)) { className[i++] = currChar; } className[i] = 0; /* null-terminate */ printf("\n className: %s\n\n", className); return 0;} 注意:当您停止阅读任何字符时,下一次阅读将在输入流中的下一个字符处继续进行.因此,如果您想从下一个字符以外的其他字符开始读取,则需要清空所有剩余字符(在该行等).另请注意,使用fopen打开文件时,应在继续从文件中读取文件之前确认该打开是否确实有效.Note: when you stop reading on any character, the next read will resume on the next character in the input stream. So you will need to empty any characters that remain (in the line, etc...) if you want to begin your read at any than other than the next character. Also note, when you open a file with fopen, you should validate that the open actually worked before proceeding to read from the file. 输入$ cat grades.txtHomeroom grades are include below 输出$ ./bin/fgetc_grades className: Homeroom希望这会帮助您入门,如果还有其他问题,请告诉我.Hopefully this will help you get started, let me know if you have any additional questions. 这篇关于如何从文件读取字符直到空格并将其存储为一个字符串(c)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!