使用fgets()读取多行。 如何进入下一行?

所以我打开一个文件,其中包含我为我的作业设计的纸牌游戏的卡片数据,基本上每行包含104个字符,每行等于一副牌。

我有一个char****因为

  1. 甲板数量
  2. 玩家人数(4)
  3. 卡数(13)
  4. 卡片表示为9H,3D表示9颗心脏和3颗钻石,因此占用2个字母空间。

我想使用fgets读取多行,但我不确定这是否有效……

for循环就是如何设置deckfile的方式,我只是想知道fgets是否会在它命中时转到下一行\n …..

  di->cards = (char****)malloc(sizeof(char***)*di->numDecks); for (i = 0; inumDecks; i++) { di->cards[i] = (char***)malloc(sizeof(char**)*4); for (j = 0; jcards[i][j] = (char**)malloc(sizeof(char*)*13); for(k = 0, kcards[i][j][k] = (char*)malloc(sizeof(char)*3); } } } for (i = 0; inumDecks, i++) { for (j = 0; j<13, j++) { for (k = 0; kdeckFile)) != NULL); } } } 

fgets()通常在循环中调用,例如:

 FILE *fp; char buf[260];// or char *buf, then use malloc - make index size appropriate length for anticipated line len. fp = fopen("C:\\somedir\\card.txt", "r"); while(fgets(buf, sizeof(buf), fp)) //where sizeof(buf) is the length of //line you anticipate reading in. { //do something with buf here; //The input of fgets will be NULL as soon //as its input fp has been fully read, then exit the loop } fclose(fp); 

你的陈述while((fgets(cards[i][k][j], 3, di->deckFile)) != NULL);
有几个问题,一个是; 在末尾。 它只会在这一行上循环,并且不会让你有机会在读取下一行之前对读取的行执行任何操作。 另外, 3可能不是你想读的行的长度,是吗? 3是保存卡数据的缓冲区大小,但是从文件中读取的会更长。

所以,考虑到所有这些,阅读评论,并根据需要进行更改,这将符合现实:)

[编辑]修改为读取文件“AS3D4C …(52卡)”4行
它将为4副卡填充足够的空间。 你可以用这个来
看看如何读取数据。 strtok(之前使用过)只在那里工作
是分隔符,如果可以,我会建议使用而不是
长串。 希望这可以帮助。
(注意,在这个例子中我没有使用[mc] alloc()。

 #include  #define FILENAME "C:\\dev\\play\\cards.txt" int main() { int i, j; FILE *fp; char buf[260];// or char *buf, then use malloc - make index size appropriate length for anticipated line len. char *cardTok; char cards[208][3]; //4 players, 4 decks, each card is 3 bytes (eg. [A|S|\0], they all need a null termination) memset(cards, 0, 208*3); fp = fopen(FILENAME, "r"); j = 0; while(fgets(buf, sizeof(buf), fp)) //where buf len is initialized at 260 //and well over the anticipated 104/line, including \n etc. { //note, fgets() will read up to n-1 characters and place a \0 at the end //but will stop reading sooner if it sees end of line. for(i=0;i<52;i++) //i is card number { cards[i+j][0] = buf[2*i+0]; cards[i+j][1] = buf[2*i+1]; cards[i+j][2] = 0; } j+=52; } fclose(fp); } 

我的文本文件如下所示:

 9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD3D4SQhKD1H9H3D4SQhKDKD1H9H3D4SQhKD 6C9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD3D4SQhKD1H9H3D4SQhKDKD1H9H3D4SQh 2D9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD3D4SQhKD1H9H3D4SQhKDKD1H9H3D4SQh 3S9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H9H3D4SQhKD1H1H9H3D4SQhKD1H9H3D4SQhKD3D4SQhKD1H9H3D4SQhKDKD1H9H3D4S 

文件说,

 char *fgets( char *str, int count, FILE *stream ); char *fgets( char *restrict str, int count, FILE *restrict stream ); 

最多读取count - 1从给定文件流中读取count - 1字符并将它们存储在str 。 生成的字符串始终以NULL结尾。 如果发生文件结束或找到换行符,则解析将停止,在这种情况下,str将包含该换行符。

也,

失败时返回值为NULL

如果故障是由EOF条件引起的,则另外在stdin上设置eof指示器(参见feof())。 如果失败是由其他一些错误引起的,请在stdin上设置错误指示符(请参阅ferror())。

同时检查feof以确保由于EOF而获得NULL

  #include  char *fgets(char *s, int size, FILE *stream); 

fgets()从流中读取最多一个小于大小的字符,并将它们存储到s指向的缓冲区中。 阅读在EOFnewline后停止。

请注意:如果读取newline ,则将其存储到缓冲区中。 终止空字节(’\ 0’)存储在缓冲区中的最后一个字符之后。

如果要比较行,则需要在空字节之前删除\n

如果你想读单行。

 char line[100]; // here you can use char *line=malloc(100); fgets(line,sizeof line,file_stream); printf("%s\n",line); 

如果你想读多行

 char lines[20][100]; // here you can use char **lines=malloc(100); i=0; //if you use **lines allocate size for all lines with the loop or else you can allocate size inside loop and then read. while((fgets(lines[i],SIZE_ALLOCATED_FOR_LINE,file_stream)!=NULL) && (i<20)) { printf("%s\n",line[i++]); } 

如果你想获取fgets输入并将其全部输入到数组或字符串数​​组中,你怎么能这样做。 我尝试了不同的东西,但得到了seg故障