本文介绍了EOF将被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C ++的新手,我必须用文件做一个项目,但我似乎有EOF问题。

Hello, I am new to C++ and I have to do a project with files, but I seem to have a problem with EOF.

该程序应该是在.txt列表中添加行和文章,直到按下CTRL ^ Z,但在我这样做之后,它不会结束。我将被提示继续添加下一行/文章。

The program is supposed to add lines and articles to the .txt list until CTRL^Z is pressed, but after I do that, it will not end. I will be prompted to continue to add the next line/article.

是否有我不知道的设置? 

Is there a setting I do not know about? 

谢谢!

#include< stdio.h>

typedef struct {
$
int cod;

char denumire [20];

float pret_mediu;

int cant [12 ];
$
} PRODUS;
$
void main()

{

FILE * f;

PRODUS x;

char nume_fisier [20];

int i;

// --- START ---

printf(" \\\
\\\
Numele fisierului:");
$
得到(nume_fisier);

if(!(f = fopen(nume_fisier," wb")))printf(" \ n \\\
Nu poate fi creat fisierul cu numele%s",nume_fisier);
$
else

{

printf(" \ nCod produs:");

scanf("%d",& x.cod);


// ---初始操作在这里结束---

while(!feof(stdin))

{

/ / ---编辑文章---

printf(&quo t; Denumire produs:");
$
fflush(stdin);

得到(x.denumire);

printf(" Pret) mediu:");

scanf("%f",& x.pret_mediu);

printf(" Cantitate lunara:\ n");

(i = 0; I< 12; i ++)

{

printf(" - luna%d:",i + 1);

scanf("%d" ;,& x.cant [i]);

}

fwrite(& x,sizeof(PRODUS),1,f);

// ---文章编辑在这里结束---

printf(" \ nCod produs:");

scanf("%d" ;,& x.cod);

}
$
// --- END ---

fclose(f);

}

}

#include <stdio.h>
typedef struct {
int cod;
char denumire[20];
float pret_mediu;
int cant[12];
} PRODUS;
void main()
{
FILE* f;
PRODUS x;
char nume_fisier[20];
int i;
//---START---
printf("\n\nNumele fisierului: ");
gets(nume_fisier);
if (!(f = fopen(nume_fisier, "wb"))) printf("\n\nNu poate fi creat fisierul cu numele %s",nume_fisier);
else
{
printf("\nCod produs: ");
scanf("%d", &x.cod);
//---initial operations end here---
while (!feof(stdin))
{
//---editing article---
printf("Denumire produs: ");
fflush(stdin);
gets(x.denumire);
printf("Pret mediu: ");
scanf("%f", &x.pret_mediu);
printf("Cantitate lunara:\n");
for (i = 0; i<12; i++)
{
printf(" - luna %d: ", i + 1);
scanf("%d", &x.cant[i]);
}
fwrite(&x, sizeof(PRODUS), 1, f);
//---article editing ends here---
printf("\nCod produs: ");
scanf("%d", &x.cod);
}
//---END---
fclose(f);
}
}

推荐答案

 老实说,自从做任何C ++以来已经很长时间了,我更喜欢C#。

 Honestly, its been very long time since doing any C++, I prefer C#.

但是,对于你的帖子,这里是我的想法... 你的while循环是

However, for your post here is my thoughts...  Your while loop is

检查STDIN的EOF。 问题在于它在

checking for the EOF of STDIN.  The problem there is that its in

二进制模式,这意味着控制Z被处理为二进制数据。 也许

Binary mode which means control Z is treated binary data.  Maybe

您可以更改while评估以检查控件Z

you can change the while evaluation to also check for the control Z

字符代码。 我不确定新的C ++是否支持捕获控制

char code.  I am not sure if newer C++ support captures of control

C和Z但是在C Sharp中你可以将它们视为正常输入。

C and Z but in C Sharp you can treat them as normal inputs.

 

 希望这会有所帮助:)

 Hope this helps :)


这篇关于EOF将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:54