问题描述
c程序
我尝试将文件保存为文本,但是当我在字符串数组中使用空格"字符时
例如
我想保存大卫黑"
仅保存了黑色",并拆分了大卫"
并在数据末尾添加
我尝试使用strcat和strcpy添加空格char("),但不起作用
有人有什么想法吗?
用某些字符(例如下划线)替换空格的方法
然后改回来....我可以得到它
但我希望这将是另一种更容易的方式
好吧,这是我的代码....有什么建议吗?
/////////////////////////////////
#define MAXDATA(20)
struct temp {
字符名称[100];
int分数;
};
void main()
{
struct temp data [MAXDATA];
int cnt;
int myscore = 50800;
cnt = data_in(data);
strcpy(data [cnt] .name,新名称");
数据[cnt] .score = myscore;
data_out(data,cnt + 1);
}
int data_in(struct temp dt [])
{
文件* fin;
int num;
if((fin = fopen("data.txt","r"))== NULL)
{
printf(文件打开错误\ n");
getchar();
exit(1);
}
num = 0;
while(fscanf(fin,%s%d",dt [num] .name,& dt [num] .score)!= EOF)
{
num ++;
}
fclose(fin);
return num;
}
void data_out(struct temp dt [],int num)
{
FILE * fout;
整数计数;
if((fout = fopen("data.txt","w"))== NULL)
{
printf("data.txt写入错误\ n");
}
for(count = 0; count< num ++)++
{
fprintf(fout,%-12s%06d \ n",dt [count] .name,dt [count] .score);
}
fclose(fout);
}
c program
I try to save file in text but when i use "space" char in string array
for example
i want to save "david black"
only "black" was saved and "david" was splited
and added at the end of data
i try to use strcat and strcpy to add space char(" ") but not work
do someone have any idea??
the way to replace space by some character (such as underscore)
and then change it back....i can get it
but i hope that it gonna be another way that is easier
well this my code.... any suggestion??
////////////////////////////////////////////////
#define MAXDATA (20)
struct temp{
char name[100];
int score;
};
void main()
{
struct temp data[MAXDATA];
int cnt;
int myscore = 50800;
cnt = data_in(data);
strcpy(data[cnt].name,"new name");
data[cnt].score = myscore;
data_out(data,cnt+1);
}
int data_in(struct temp dt[])
{
FILE *fin;
int num;
if((fin = fopen("data.txt","r")) == NULL)
{
printf("FILE OPEN ERROR\n");
getchar();
exit(1);
}
num = 0;
while (fscanf(fin,"%s%d", dt[num].name,&dt[num].score)!=EOF)
{
num++;
}
fclose(fin);
return num;
}
void data_out(struct temp dt[], int num)
{
FILE *fout;
int count;
if((fout = fopen("data.txt","w")) == NULL)
{
printf("data.txt write ERROR\n");
}
for(count=0;count<num;count++)
{
fprintf(fout,"%-12s%06d \n", dt[count].name, dt[count].score);
}
fclose(fout);
}
推荐答案
我要保存大卫黑"
仅保存了黑色",并拆分了大卫"
并添加到数据末尾
i want to save "david black"
only "black" was saved and "david" was splited
and added at the end of data
您不会显示用于尝试实现此目的的代码.也许如果您发布程序中可以节省的部分,我们可以提供一些帮助.
You do not show the code you used to try and achieve this. Maybe if you post the part of your program that does the save we can offer some assistance.
这篇关于fprintf,fscanf?使用“空格"保存/加载问题特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!