fprintf,fscanf?使用“空格"保存/加载问题特点 [英] fprintf,fscanf?? save /load problew with "space" character

查看:120
本文介绍了fprintf,fscanf?使用“空格"保存/加载问题特点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
}

推荐答案

printf没问题:例如,该函数正确输出字符串大卫黑" . /> 另一方面,fscanf将仅返回字符串的第一部分,即"david" .
一个简单的(并且是天真)解决方法是对空格字符进行转义,例如,下划线(''_''):您可以将"david_black" 编写为文件,然后在使用fscanf进行检索时,将下划线替换为空格.
:)
There is no problem with printf: for instance, the function outputs correctly the string "david black".
fscanf, on the other hand, would return only the first part of the string, namely "david".
A simple (and naive) work-around would be an escape for the space character, for instance, the underscore (''_''): you could write "david_black" to the file and then, on retrieval with fscanf, replace back the underscore with the space.
:)


写道:​​

我要保存大卫黑"
仅保存了黑色",并拆分了大卫"
并添加到数据末尾

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?使用“空格"保存/加载问题特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆