我正在尝试打开然后写入 .dat 文件。该文件只是一系列简单的数字,但我想添加它。现在 fputs 不适合我。
我想知道我是否使用正确的功能来完成这项工作。现在它说我不能 在函数fputs中使用整数enter_this因为它不是一个常量字符。
我想要求用户在文件中添加一个整数。了解这一点后,我的下一步是添加字符串、浮点数、字符等。但只要得到一些有效的东西就很好。
#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include
//functions called
//why is it void?
int main(void)
{
FILE *pFile;
int choice = 0;
char buf[40];
int i = 0;
int num[40];
int enter_this;
printf("WELCOME. \n\n");
pFile = fopen("test.dat", "r");
if (pFile != NULL)
for (i = 0; i < 8; i++)
{
//get num
fgets(buf, sizeof(buf), pFile);
num[i] = atoi(buf);
printf("#%i = %i\n", i, num[i]);
}
printf("Enter number to be added: ");
gets_s(buf);
enter_this = atoi(buf);
fputs(enter_this, pFile);
fclose(pFile);
system("pause");
}//end main