Click here to Skip to main content
15,915,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
Posted
Updated 30-Dec-09 17:53pm
v2

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.
:)
 
Share this answer
 
v2
wrote:
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900