PDA

Click to See Complete Forum and Search --> : letter onto a string


richy
May 9th, 2001, 02:09 PM
Should be quite simple this...
I'm doing some C coursework (using Borland), and I've read a character from a text file. I now want to put the letter onto the end of the string, but everytime I keep getting the same error. I've tried using strcat but that doesn't work either.

Heres the basics of the code

char letter;
char reading[30];
FILE *fp;

fp=fopen("names.txt","r");

letter=fgetc(fp);
strcat(reading,letter);


Can anybody tell me what the problem is and how I can get this working.

SteveCRM
May 9th, 2001, 02:27 PM
try sprintf its the same syntax as strcat ;)

richy
May 9th, 2001, 03:09 PM
No thats not working either. I'm getting the error of Cannot convert 'int' to const char *'
Any more ideas anyone?

parksie
May 9th, 2001, 03:12 PM
char letter;
char reading[30];
FILE *fp;

fp = fopen("names.txt", "r");

letter = fgetc(fp);
sprintf(reading, "%c", letter);

HarryW
May 9th, 2001, 03:13 PM
strcat() isn't working because it's expecting two pointers to strings as parameters, so it can concatenate the second string onto the end of te first one. What you are passing into the function needs to be of type char *, and you are passing a single character, of type char.

Also, the char[] variable you have called 'reading' should be initialised with a string. The strcat function will look for the end of the string so it knows where to start addign bits on, and if you haven't initialised it with a string you can't be sure where, or if, there will be an end-of-string character (ASCII code of 0, the character '\0').

sprintf() is probably much easier to use for most purposes. It works just like printf() but the first argument is the string variable to print the formatted string to.

Anyway, I don't know if you're assigning an initial string to reading. If you want to assign a value to it you can use the strcpy() funtion.

richy
May 9th, 2001, 03:23 PM
Thanks everyone for your help. Thats got it sorted, so I'll carry on doing my project, until the next thing goes wrong (probably in about 10 minutes :-( ), when it all goes wrong again.

Thanks again.

richy
May 9th, 2001, 04:03 PM
Bad news people.
I've just tried my code out and the sprintf seems to be overwriting the string rather than adding the letter onto the end. any ideas?

parksie
May 9th, 2001, 04:06 PM
It's putting it at the start. Since your code doesn't seem to load anything from the file, there's nothing else to put down :confused:

richy
May 9th, 2001, 04:11 PM
This is my code rattled down a little. On the puts line it is only outputting the last letter that its loaded from the file.

e.g.

T
e
s
t

where it should be doing

T
Te
Tes
Test

{shouldn't it?}

while(feof(fp)==0)
{
letter=fgetc(fp);
sprintf(reading,"%c",letter);
puts(reading);
}

HarryW
May 9th, 2001, 05:15 PM
This is why I mentioned the fact that you aren't initialising your string. sprintf() just overwrites whatever you have in the string already. You can get around this by writing what's already in the string and then something else:
while(feof(fp)==0)
{
letter=fgetc(fp);
sprintf(reading,"%s%c", reading, letter);
puts(reading);
}
You still need to initialise the string and make it empty to begin with though. Just change your declaration to this:
char reading[30] = "";
or thischar reading[30];
reading[0] = '\0';



PS please use the tags to format your code, it's much much easier to read.