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