#include <stdio.h>
#include <stdlib.h>
#include<iostream>
int main()
{
int a=100;
char str[] = "hello";
FILE *fp = NULL;
fp = fopen("test.txt", "w");
if (fp == NULL)
{
printf("Cann't open the file!");
exit(1);
}
else
{
fprintf(fp, "%s,%d", str,a);//把字符数组str的内容输到文件fp中去
fclose(fp);
}
system("PAUSE");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#define STR_LEN 1024
int main()
{
int b=0;
char str2[STR_LEN] = {0};
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Cann't open the file!");
exit(1);
}
else
{
fscanf(fp,"%s%d",str2,&b);//把文件fp中的内容以字符串的形式存到字符数组str2中
printf("%s%d\n",str2,b);
fclose(fp);
}
system("PAUSE");
return 0;
}
上面的内容,输出结果总是不理想,请看看哪里有错。我用的DEV编译器,第一个程序是向文件输入,第二个是从文件到变量的传输。
第二个程序运行之后,变量a从文本传回来之后的值竟然变成1000了,请看看是哪里错了