![](/img/trans.png)
[英]How to read multiple lines of input from user using fgets and write it into a file using fputs in C?
[英]Using fgets() to read multiple lines. How to go to the Next line?
所以我正在打开一个文件,其中包含我为我的任务设计的纸牌游戏的纸牌数据,基本上每行包含 104 个字符,每行等于一副纸牌。
我正在使用char ****
因为
9H
, 3D
表示九颗心和三颗菱形,因此使用 2 个字符。 我想使用fgets()
来读取多行,但我不确定这是否有效......
for
循环只是设置deckfile 的方式,我只想知道fgets
在遇到\\n
时是否会转到下一行...
di->cards = (char ****)malloc(sizeof(char***) * di->numDecks);
for (i = 0; i < di->numDecks; i++) {
di->cards[i] = (char ***)malloc(sizeof(char**) * 4);
for (j = 0; j < 4, j++) {
di->cards[i][j] = (char **)malloc(sizeof(char*) * 13);
for (k = 0, k < 13, k++) {
di->cards[i][j][k] = (char *)malloc(sizeof(char) * 3);
}
}
}
for (i = 0; i < di->numDecks, i++) {
for (j = 0; j < 13, j++) {
for (k = 0; k < 4; k++) {
while ((fgets(cards[i][k][j], 3, di->deckFile)) != 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 <ansi_c.h>
#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
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
fgets()
从流中读取至多小于 size 的一个字符,并将它们存储到 s 指向的缓冲区中。 阅读在EOF
或newline
后停止。
请注意:如果读取newline
,则将其存储到缓冲区中。 终止空字节 ('\\0') 存储在缓冲区中的最后一个字符之后。
当你想比较 line 之前,你需要在空字节之前删除\\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++]);
}
文档说,
char *fgets( char *str, int count, FILE *stream );
char *fgets( char *restrict str, int count, FILE *restrict stream );
从给定的文件流中最多读取
count - 1
字符并将它们存储在str
。 生成的字符串始终以 NULL 结尾。 如果出现文件结束符或找到换行符,解析将停止,在这种情况下 str 将包含该换行符。
还有,
失败时返回值为NULL
。
如果失败是由EOF
条件引起的,则在 stdin 上另外设置 eof 指示符(参见 feof())。 如果失败是由某些其他错误引起的,请在 stdin 上设置错误指示符(请参阅 ferror())。
还要检查feof以确保由于 EOF 获得了NULL
如果您想获取 fgets 输入并将其全部输入到数组数组或字符串数组中,您该怎么做。 我尝试了不同的东西,但遇到了段错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.